This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Are Session thread safe. 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 "Are Session thread safe." Watch "Are Session thread safe." New topic
Author

Are Session thread safe.

harishkumar mehra
Greenhorn

Joined: Oct 22, 2009
Posts: 2
could anyone clarify my doubt that whether the sessions are thread safe or not.?
Ram Para
Ranch Hand

Joined: Jul 09, 2008
Posts: 50
HttpSession is accessible only to the threads that are servicing requests belonging to
that session. We may think that there can be only one request from the user at a time and,
therefore, the session scope would be thread safe; however, that’s not the case. A user can
open multiple browser windows and send requests through multiple windows. In such
instances, all the requests belong to the same session and all the threads processing these
requests will be able to access the session attributes simultaneously.


Ram Parashar
http://doinfinite.com
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

And multiple windows may not be needed if you use AJAX techniques. From the servlet specification (always worth a read):

Multiple servlets executing request threads may have active access to a single
session object at the same time. The Developer has the responsibility for
synchronizing access to session resources as appropriate.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
Ram Para wrote:HttpSession is accessible only to the threads that are servicing requests belonging to
that session. We may think that there can be only one request from the user at a time and,
therefore, the session scope would be thread safe; however, that’s not the case. A user can
open multiple browser windows and send requests through multiple windows. In such
instances, all the requests belong to the same session and all the threads processing these
requests will be able to access the session attributes simultaneously.


Hi, If this is the case, my session attribute values are accessed by many threads which can cause modification in the session attribute values. If my application is navigating pages based on the session attributes, What will happen?



Abu.A
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
What will happen depends on how all these threads are handling the session data. If one thread is modifying while another is reading you have obvious problems. Easily avoided by synchronizing on the session object and paying close attention to how objects with references in the session are used.

On the other hand if all threads are just reading....

Bill
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90


Thanks William,

So HttpSession Object is not thread safe? We should take precautions...



Abu.A
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41144
    
  45
More useful than asking "is this object thread-safe?" is the question "is this code thread-safe?". As William said, objects can be used in thread-safe and thread-unsafe ways. But if an object is used in multiple threads, and one of the threads modifies it, then the code is not thread-safe and precautions must be taken.


Ping & DNS - my free Android networking tools app
azhar jodatti
Greenhorn

Joined: Oct 18, 2009
Posts: 12
no the session variable is not thread safe.....




i too have some problem, it might be very easy but i am confused a lot...

imagine a scenario....
i have a bean which representing the customer or anyprocess(beans means any type bean)
if i am using the servlets as a client to the bean then first request come then it process it.. its fine

but what about second ,third fourth on.... is i need to lookup for the bean in each servlet...
how could i ever get a bean which is created in first request...

please help me out.....


..FEEL IT..
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
hey folks!

Do you synchronize session objects? Sometimes? Always?

Thank you!
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
Hi Ulf,



The above scenario which azharuddin asked, Is it taken care of web server that serves the incoming http request? or

We need to do explicit measures to protect the code thread safe like HttpSession?

Abu.A
azhar jodatti
Greenhorn

Joined: Oct 18, 2009
Posts: 12
hii..

my last post on EJB was not related to whether sssion variable are thread safe or not.. i am confused about what abbubakar is posted............ i was just thinking about beans......... how i can get same bean in diffrent reqeust.. please help me out.
Abubacker Siddik
Ranch Hand

Joined: Aug 12, 2009
Posts: 90
Hi i am sorry, I misunderstood your problem. You take a look at this.

EJB Thread safe

Abu
Pavan Kumar Reddy
Greenhorn

Joined: Dec 11, 2009
Posts: 13
mulitple threads accesing simulteneoulsy session attributes means sharing the session,so session is not thread safe,if we want to put a behaviour like session in thread safe you have to use synchronized keyword like synchronized(session)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Are Session thread safe.
 
Similar Threads
thread safe
vetor thread safe?
threadsafe objects
question from the jweb+
ActionServlet Thread Safe