aspose file tools*
The moose likes Servlets and the fly likes Session Object ArrayList Attribute and Thread Saftey Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session Object ArrayList Attribute and Thread Saftey Issue" Watch "Session Object ArrayList Attribute and Thread Saftey Issue" New topic
Author

Session Object ArrayList Attribute and Thread Saftey Issue

James Ellis
Ranch Hand

Joined: Oct 14, 2004
Posts: 205
I have this issue which only appears in production. I suspect a threading issue since it only appears under high load.

In my code there is a Requirements bean:



...that is set into session by a servlet. Right after that servlet sets that bean into session it includes a JSP page which has embedded Ajax which upon loading makes an Ajax call back to the server. This Ajax call calls another servlet on the server which references the Requirements object that was put into session. Once in a while that Requirements object is missing data (or possibly completely missing).

Here is the pseudocode:



Two Questions:
1) When you set a session attribute from a servlet, will that method call "setAttribute" guarantee to finish before the servlet's doPost?
2) When you add members to an ArrayList (rolesToAdd), are these guaranteed to be added by the time the ArrayList is added to the requirements object (which is then added itself to session)?

I know that ArrayList is not synchronized...but I'm not sure if that means that calls to ArrayList.add("obj") are guaranteed to finish before caller moves on. I have the same concern about session.setAttribute, not sure if calls to setAttribute are guaranteed to finish before caller moves on.
[ July 26, 2008: Message edited by: James Ellis ]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

FYI to those responding to this thread, it is also being discussed on the forum.sun.com forums: Java Servlet - Session Scope Attribute ArrayList Threading Question


Steve
 
jQuery in Action, 2nd edition
 
subject: Session Object ArrayList Attribute and Thread Saftey Issue