>>> Peet DENNY <[EMAIL PROTECTED]> 31-Jan-00 3:35:29 PM >>>
>Which raises the question: How visible are the variables in a
servlet?
visible is a word applied to scope.
Are you really talking about scope or concurrency?
public class A
{
//class or instance scope
int a=0;
public void method()
{
//local or method scope
int b=0;
}
}
>Can everybody potentially write over the variables of everybod
else?
If you're talking about scope no... but I suspect you're talking
about concurrency.
>If two customers are both shopping at the same time, sharing the
products
>array, then products = db.getProducts("sand blasters") will write
over
>whatever product array the other user had done a search for.
If you keep that array at class scope then you have a problem. There
is only one instance of each servlet class. Therefore if you store
data for users in a class-scope variable you must ensure that user's
don't overwrite each others data.
> I'm trying to
>overcome this by binding the product array to the session so I can
do this:
If the data you create is all created within the scope of the doGet()
(or whatever) method then you can't have a problem with concurrency.
>What happens though if in between
> products = session.getValue("products");
and the next bit, somebody stomps in with a
> products=db.getProducts("Scouse beer mats"),
As long as products and db is locally scoped to the doGet (or
whatever) method you're ok. The two operations occur in seperate
threads.
>Do we need to go as far as synchronising (haven't done any of this
yet) the
>retreival of the products array from the clients session, putting
the results
>of a database search in it , and putting it back in the session?
Dangerous Session object operations are intriniscally synchronized
because the servlet engine *knows* that you are going to be sharing
access to the session table across threads. That's what it's there for
after all.
>What I mean by "synchronising" is: locking access to the variable
>until I've carried out all the necessary on it so that nobody else
can
>corrupt the data. Is that what "synchronising" means, or am I
>thinking of something else?
That's pretty much it. A synchronized block of code ensures that only
one thread at a time can execute it.
Hope that helps.
If you're still confused try reading the spec or one of the FAQs...
they give more detail about servlet concurrency.
Nic Ferrier
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html