-
Session and Synchronization issue (8 messages)
- Posted by: Abhishek Sharma
- Posted on: November 02 2006 01:55 EST
I found following comments from struts based code: "Servlet containers are allowed to return a different HttpSession objectfor two threads accessing the same session so it is not possible to synchronize on the session" Anybody could explain me what he is trying to say by above comment.Threaded Messages (8)
- Re: Session and Synchronization issue by arijit dey on November 02 2006 23:38 EST
- Re: Session and Synchronization issue by Amit L on November 03 2006 05:51 EST
- Re: Session and Synchronization issue by Rene Zanner on November 07 2006 09:37 EST
- Re: Session and Synchronization issue by Amit L on November 03 2006 05:51 EST
- Re: Session and Synchronization issue by Abhishek Sharma on November 13 2006 05:40 EST
- Re: Session and Synchronization issue by Chu Tan on November 14 2006 10:44 EST
- Should you synchronize access to HttpSession? by Valery Tydykov on November 26 2007 16:16 EST
- issue by club stork on October 15 2012 02:15 EDT
- issue by matt coleman on November 05 2012 00:48 EST
-
Re: Session and Synchronization issue[ Go to top ]
- Posted by: arijit dey
- Posted on: November 02 2006 23:38 EST
- in response to Abhishek Sharma
Every object has a lock associated with it.If two threads want to synchronize on some object, that object has to be the same.In the case of Servlets, if two threads are trying to access the same session, the servlet may give them different session object, Now if the two thread want to sysnchronize on session object, both of them will have different objects and two different locks, so they will not block for each other or I can say synchronization will fail. Am I clear or I am still fuzzy? cheers, http://www.javaicillusion.blogspot.com/ -
Re: Session and Synchronization issue[ Go to top ]
- Posted by: Amit L
- Posted on: November 03 2006 05:51 EST
- in response to arijit dey
Hello, I have one question on this. Why the servlet or the container would return two different objects of the session ? Whenever we set some attribute in the session if servlet/container then we might be getting different session object. If the answer is YES then I have another question that who will maintain two or number of copies of session. regards, Amit -
Re: Session and Synchronization issue[ Go to top ]
- Posted by: Rene Zanner
- Posted on: November 07 2006 09:37 EST
- in response to Amit L
Hi, I think this sentence just is wrong. A distributed environment (i.e. cluster) is the only place where it can occur that two separate requests belonging to one specific session get associated with two different session objects. This may be the case when between those two requests the original serving web server is not able to handle the next request belonging to this session. Provided the session was replicated to a different member of the cluster this different server handles all subsequent requests for this specific session. In this distributed scenario a synchronization on the TokenProcessor instance - as it is implemented now - would also be useless. Since the other server has a completely different VM, this argument is not valid, because there would be two completely independent TokenProcessor classes. To make it short: I do not know WHY this comment is written there. Usually you can rely on the fact that the container returns the same HttpSession instance for subsequent requests belonging to the same session. One statement found in the Servlet 2.3 spec in chapter "Threading issues" on page 52 to support my opinion:Multiple servlets executing request threads may have active access to a single session object at the same time.
Cheers, René -
Re: Session and Synchronization issue[ Go to top ]
- Posted by: Abhishek Sharma
- Posted on: November 13 2006 05:40 EST
- in response to Abhishek Sharma
Hi Rene, May be you are correct but same time statement is saying that you can't Synchronize two session object and i found some code related to TokenProcessor where ppl Synchronizing session id rather than session object in multiple form submission problem. Abhishek -
Re: Session and Synchronization issue[ Go to top ]
- Posted by: Chu Tan
- Posted on: November 14 2006 10:44 EST
- in response to Abhishek Sharma
Here's my solution. It guarantees that the set of objects A, B, C in session do not have synchronization issue. public void setInSession(Object a, Object b, Object c){ synchronized (getSessionMutex("ABCMutex")){ HttpSession sess = getSession(); sess.setAttribute("a", a); sess.setAttribute("b", b); sess.setAttribute("c", c); } } public void clearABC(){ synchronized (getSessionMutex("ABCMutex")){ HttpSession sess = getSession(); sess.removeAttribute("a"); sess.removeAttribute("b"); sess.removeAttribute("c"); sess.removeAttribute("ABCMutex"); } } /** NOTE: This method is NOT 100% threadsafe! But risk is lower than not implementing this. */ private Object getSessionMutex(String id){ HttpSession sess = getSession(); Object mutex = sess.getAttribute(id); if (mutex == null){ mutex = new Object(); sess.setAttribute(id, mutex); } return mutex; } -
Should you synchronize access to HttpSession?[ Go to top ]
- Posted by: Valery Tydykov
- Posted on: November 26 2007 16:16 EST
- in response to Abhishek Sharma
The short answer is: No. The Java Servlet 2.5 specification was recently clarified on this topic: http://www.jroller.com/galina -
issue[ Go to top ]
- Posted by: club stork
- Posted on: October 15 2012 02:15 EDT
- in response to Abhishek Sharma
I tried the codes above and it still not working..help!
-
issue[ Go to top ]
- Posted by: matt coleman
- Posted on: November 05 2012 00:48 EST
- in response to Abhishek Sharma
Issues will always arise from any program..what matters is that you address it