• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is session attribute thread-safe?

 
Siripa Siangklom
Ranch Hand
Posts: 79
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I visit the same web application across two browser windows simultaneity, there will be two threads visiting the web application.
But I don't know whether the two threads share the same session?
If they share, the session attribute is not thread-safe.
If they do not share, the session attribute is thread-safe.
who can explain it to me?
 
Basavaraju Banakar
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The requests from different browser instances (other than opening a new window form an existing one) will have diferent sessions.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 'two browser windows sharing session' debate always seems to relate to how 'well behaved' your particular browser is.

I think it was Netscape 4.x that would simply NOT be able to have two different sessions.

Also, current versions of IE will *always* share a session if you use "File|New|Window", "Cntrl-N" or any other variation of opening a second window from an existing window.

If, on the other hand, you click the Start Menu icon twice, or you click the QuickLaunch toolbar icon twice, then you will get two windows of IE that know nothing about each other, and will NOT share a session.


As for thread safety, the issue *can* be most easily demonstrated by having two browser windows that share a session. But it's not limited to this case.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 2 browsers will represent 2 sessions.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Curwen:
If, on the other hand, you click the Start Menu icon twice, or you click the QuickLaunch toolbar icon twice, then you will get two windows of IE that know nothing about each other, and will NOT share a session.

It's very good infomation... I've never known it b4... I think the following thread might be useful to the original poster as well....
http://www.coderanch.com/t/357781/Servlets/java/block-multiple-logins-same-user
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply put:
if there are two instances of the browser executable running on the client, each of which accesses the web application, those two should have two distinct sessions (indeed Netscape 4 may have been extremely misbehaved in this, but it is possible that it simply was too smart for itself and started another window in the same instance whenever you thought you were launching a new instance).
If there are two windows running in the same browser instance both will share the session.
You can easily check this on Windows in the taskmanager.
Launch IE and you will see a task 'iexplore.exe' appearing. Open a new window from the menu (or click Ctrl-N), no new task will appear.
Kill the task and both windows disappear.
Now launch IE twice from the desktop, quicklaunch bar or startmenu. You'll see 2 tasks 'iexplore.exe'.
 
Kevin Jones
Author
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But this doesn't make your sessions thread safe. If you 'refresh' the window this sends a request to the server which could access the session, hitting refresh again before the first request finishes will send another request to the server and more session access.
Never rely on thread safety in sessions, always synchronize
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kevin Jones:
But this doesn't make your sessions thread safe. If you 'refresh' the window this sends a request to the server which could access the session, hitting refresh again before the first request finishes will send another request to the server and more session access.
Never rely on thread safety in sessions, always synchronize

In case we use frames we encounter synchronization problem.
[ January 30, 2004: Message edited by: Pradeep Bhat ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any web page generation that involves multiple simultaneous requests will have to watch out for synchronization problems. Not only frames but also images or other resources generated on the fly can cause a servlet to be processing multiple requests from the same browser instance "at the same time."
Bill
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic