Hi :-) please see intermix
Paul Foxton wrote:
> Hi all,
>
> I've been wondering about the fact that concurrent requests to a servlet
> will result in a new thread being created to access the same instance of the
> servlet as long as singleThreadModel is not implemented with reference to
> the following:
>
> If you have a servlet which accesses a database, and request parameters from
> an HTML form are used to build the SQL where clause, if these request
> parameters are stored in class instance variables (i.e. declared at the
> beginning of the class and outside of any methods, so class scope), would
> this mean that there is a possibility of data being overwritten in the
> middle of 2 or more concurrent requests and therefore compromising the
> integrity of the data retrieved from the database?
>
it is possible: several Threads(made with the same instance of MyServlt)
will "share"-(for example, make loacal working copy of) the instance
variables(which is in heap-memory) of MyServlet. with the similar reason,
static fields in MyServlet will also be "shared". from several emails,
now it is possible that the different clients of MyServlet get
"mixed- responses".
>
> If so, I would have thought that the way around this is to store the values
> from the request parameters in variables declared inside methods, which
> would then have block scope within that method.
>
yes, if a static/instance method is "used" by sevaral Threads, I guess several
"working
copies" of this method will be made in "stack-memory", so the loacal/method
variables
will not be "shared" by several Threads.
>
> But surely, if two requests (threads) run through this block at the same
> time there is still a possibility of a problem with data integrity?
>
no, but please notice that this local/method can not refrence to a
instance/static
field of MyServlet, or refrence to a object shared by several Threads, for
example:
doGet(...){
...
HttpSession session=req.getSession();
session.getAttibute(...)/setAttibute(...);
...
}
session is a local variable, but because it references to req.getSession(), so
I think "synchronized" need to be used here
>
> This doesn't seem quite right to me tho, because then you would have to
> synchronize access to any method or variable which was used to build an sql
> query or update.
>
> Can anyone clarify?
>
> Sorry if this is an obvious question. I've read through the documentation on
> threads in servlets at Suns java site, but am still a little confused.
>
> Thanks,
>
> Paul
> [...]
Bo
July 16, 2001
___________________________________________________________________________
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