Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are Session thread safe.

 
harishkumar mehra
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could anyone clarify my doubt that whether the sessions are thread safe or not.?
 
Ram Para
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abubacker Siddik
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Thanks William,

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



Abu.A
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
azhar jodatti
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey folks!

Do you synchronize session objects? Sometimes? Always?

Thank you!
 
Abubacker Siddik
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i am sorry, I misunderstood your problem. You take a look at this.

EJB Thread safe

Abu
 
Pavan Kumar Reddy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic