Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread safe about session and request scope

 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I would like to clarify the concept of thread safe about session and request scope, if I am wrong, please feel free to tell me.

For session, it is non-thread safe because if 2 threads, tA1 and tA2, access same servletA simutanously, the session object comes from both request and is refered by both reference. So both reference refer to the same session object.

For request, it is thread safe because when tA1 and tA2 access same servletA, both request will be generated from servlet container and pass to the service method of servletA. So both reference of request refer to different request object.

Am I right?

Thanks in advance!
Jack
 
Shiang Wang
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right.
Multiple requests might access the same session, that is why session is not thread safe. While at any given time, only one thread can process one request, even if it forwards to a different servlet/jsp, they share the same attributes that are in scope of request.
 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
Jack
 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
um, I still do not understand that if request 1 and request 2 are coming from different clients, client 1 and client 2 respectively, they actually have different session (e.g. different session id). So the objects in session scope should be thread safe?
 
Mikalai Zaikin
Ranch Hand
Posts: 3367
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jack Lau:
um, I still do not understand that if request 1 and request 2 are coming from different clients, client 1 and client 2 respectively, they actually have different session (e.g. different session id). So the objects in session scope should be thread safe?


Hi Jack,

objects in session scope are NOT thread safe, you can have SEVERAL *browser instances* running on the same machine having same session ID cookies, thus participating in the same session.

How to emulate this situation is described here.

regards,
MZ
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic