This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
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
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
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.
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 ]