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 synchronize session 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 "synchronize session" Watch "synchronize session" New topic
Author

synchronize session

Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, I was wondering if we need to synchronize session whenever we make any changes to its attribute values so that concurrent access to the same session will not corrupt the attribute values?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
I think that will depend on the way HttpSession is implemented in your system. Looking at the Tomcat source code, I see that a Hashtable is used to store data, so you would not need to synchronize simple get and set operations, but you would need to synchronize anything more complex, such as getAttributeNames()
To be sure, you should synchronize if your design is such that more than one request will be processed at one time.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by William Brogden:
To be sure, you should synchronize if your design is such that more than one request will be processed at one time.

Whether more than one request will be processed at one time is not just a matter of design. You cannot stop a user from pressing Refresh. If there's anything in your servlet/JSP which may take some time -- intensive computation, database access -- there is a real chance that a single session may see multiple requests at a time.
Moreover, if you've got a site with frames, multiple simultaneous requests are virtually guaranteed. Browsers often open up a couple of connections to load up all the documents in a frameset.
- Peter
 
Consider Paul's rocket mass heater.
 
subject: synchronize session
 
Similar Threads
Thread Safe Issue
Do I need to synchronize session in this case?
synchronized(session) will really help...?????
Can some one help me to understand answer to this question in the Head-first JSP and servlet book?
Share session between web apps