Win a copy of Learn Java with Math: Using Fun Projects and Games this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Issues with threads, sessions and requests

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 67313
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!