Thanks very much Hans, and thanks for such a clear explanation.

I have a tendency to think myself into corners sometimes ;�)

> -----Original Message-----
> From: Hans Liebenberg [mailto:[EMAIL PROTECTED]]
> Sent: 16 July 2001 15:58
> To: [EMAIL PROTECTED]
> Subject: Re: concurrent access to variables with class or block scope
> 
> 
> Hi Paul,
> 
> Your question relates more to general programming then servlets
> specifically.
> If from with in a method you create an object x:
>  MyObject x = new MyObject()
> 
> It doesn't matter how many threads access the members of x as 
> long as each
> one create a new instance of x as above.
> 
> >>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?
> 
> ..method variables never share the same address space for 
> multiple threads.
> The only time you may have this is if your method is declared 
> as static
> accessing static variables.
> 
> As far as multi threaded servlets are concerned you should 
> never have member
> variables unless they are to be shared amongst all threads.
> 
> 
> So for your servlet, rather create an object that handles 
> your for request
> say MyRequestHandler, and create a new Instance of the 
> RequestHandler for
> each thread (as above) and you can have as many member 
> variables as you want
> :)
> 
> -----Original Message-----
> From: A mailing list for discussion about Sun Microsystem's 
> Java Servlet
> API Technology. [mailto:[EMAIL PROTECTED]]On 
> Behalf Of Paul
> Foxton
> Sent: 16 July 2001 15:22
> To: [EMAIL PROTECTED]
> Subject: concurrent access to variables with class or block scope
> 
> 
> 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?
> 
> 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.
> 
> 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?
> 
> 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
> 
> ______________________________________________________________
> _____________
> 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
> 
> ______________________________________________________________
> _____________
> 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
> 

___________________________________________________________________________
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

Reply via email to