wood burning stoves 2.0*
The moose likes Servlets and the fly likes Thread Safe Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Thread Safe Issue" Watch "Thread Safe Issue" New topic
Author

Thread Safe Issue

vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
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.

Thanks
Vipul Kumar
Stoian Azarov
Ranch Hand

Joined: Jun 01, 2011
Posts: 113
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

Joined: Oct 14, 2010
Posts: 218


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

Joined: Jun 01, 2011
Posts: 113
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

Joined: Oct 14, 2010
Posts: 218
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

Joined: Mar 22, 2000
Posts: 12792
    
    5
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

Joined: Jun 01, 2011
Posts: 113
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

Joined: Jan 10, 2002
Posts: 61316
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
vipul bondugula
Ranch Hand

Joined: Oct 14, 2010
Posts: 218
Thanks Bear,Stoian and bill.
 
GeeCON Prague 2014
 
subject: Thread Safe Issue