• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Safe Issue

 
vipul bondugula
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,


I remember in a thread Mr.Bear told," Modifying values in session and application scope can also create Thread safety issues ".

"Modifying values in session" means what type of values he means here.Can anyone tell me..

Thanks,
Vipul.
 
Stoian Azarov
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Session and application attributes can raise an thread safety issues.
So every time you change or read these attributes you has to synchronize HttpSession or ServletContect objects(or use other synchronization techniques).
 
vipul bondugula
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Each user will have his own session to interact with the server.
There will be no multiple users interacting in a single session with the server.
This means , we no need to synchronize session.


Correct me if iam wrong.

Thanks,
Vipul Kumar.
 
Stoian Azarov
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about two browser windows(tabs) that are using the same session?
That way you will have the same session and 2 separate requests(having access to that session).
 
vipul bondugula
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Question..

2 requests will not come at a time to server.Simultaneously the request will come.so, there is no chance of threading issue.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, a single browser web page can be making multiple requests to an application "at the same time" so a given HttpSession object can be in use in multiple request Threads. It is up to the programmer to decide if these uses constitute a safety problem.

If all the requests are just reading (not modifying) session data - no problem

Bill
 
Stoian Azarov
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am pretty sure that session object is not thread safe. You cannot be sure which request will come first and if they would not be processed by the container at the same time. The container is instantiating separate threads for every request, and there is no guarantee that these threads will execute one after another(they might run concurrently).

I think that I found the source where this problem was explained in great details: Head First Servlets & JSP by Bryan Basham, Kathy Sierra, Bert Bates - Second Edition - Chapter 5 Being a Web App - page 200
The title of the page is: Protect session attributes by synchronizing on the HttpSession.

Regards
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vipul bondugula wrote:Each user will have his own session to interact with the server.
There will be no multiple users interacting in a single session with the server.
This means , we no need to synchronize session.

Correct me if iam wrong.

You are wrong.

The same user can have multiple requests coming into the server at the same time by way of Ajax requests, multiple browser windows, framesets, iframes, and the like.

If these requests are all reading and writing values to the session, there can be contention issues.
 
vipul bondugula
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear,Stoian and bill.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic