*
The moose likes Servlets and the fly likes Issues with threads, sessions and requests Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Issues with threads, sessions and requests" Watch "Issues with threads, sessions and requests" New topic
Author

Issues with threads, sessions and requests

Simon Harvey
Ranch Hand

Joined: Jan 26, 2003
Posts: 79
Hi,
This question is related to and follows on from the one immediately below.
The post below raised the issue that session objects are not thread safe but request objects are.
The thing is, how can a session object not be thread safe considering it is only related to one user? Is is because that user could in theory open more than one window and make another request or something?
At the end of the day what should we do to cope with this. Threading is an issue that i think is completely underdocumented. I have made a consistent effort to understand how to cope but then something like this comes up and screws everything up again.
Essentially, the most important thing I am wondering about how to deal with session objects then? Should all methods that do something with the session object be synchronised?
I just really don't get what needs synched and what doesnt these days.
Thanks to anyone who can help

Simon
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65


The replies in the topic you reference cover this. Essentially, even a single user is not limited to one window, so there can be multiple threads accessing the same session at the same time.
Any access to session objects that might cause problems if executed simultaneously should be synchronized.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Is the collection that implements HttpSession.getAttribute() and HttpSession.setAttribute() guaranteed to implement those operations atomically?
If not, I don't see how to easily make a JSP thread-safe if it contains any <jsp:setProperty> and <jsp:getProperty>tags that reference beans in session scope.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Manjunath Subramanian
Ranch Hand

Joined: Jul 18, 2001
Posts: 236
Originally posted by Simon Harvey:

Essentially, the most important thing I am wondering about how to deal with session objects then? Should all methods that do something with the session object be synchronised?

Well..You need not synchronize the entire method, you could synchronize only that code which uses the session objects.Also, since the entire servlet and jsp will not have code related to session objects, this should not be a problem.
Keep in mind that large objects should not be stored in session objects because of performance issues.
[ July 22, 2003: Message edited by: Manjunath Subramanian ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issues with threads, sessions and requests
 
Similar Threads
Is session object thread safe.
I know local objects are thread safe. But why not obj here too?
is it good to carry the jdbc "Connection" over servlet and Actions ?
local variable Vs class level variable
servlet session thread safety ?