Session and Synchronization issue

Discussions

Web tier: servlets, JSP, Web frameworks: Session and Synchronization issue

  1. Session and Synchronization issue (6 messages)

    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 (6)

  2. 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/
  3. Re: Session and Synchronization issue[ Go to top ]

    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
  4. Re: Session and Synchronization issue[ Go to top ]

    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é
  5. 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
  6. Re: Session and Synchronization issue[ Go to top ]

    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; }
  7. The short answer is: No. The Java Servlet 2.5 specification was recently clarified on this topic: http://www.jroller.com/galina