tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Shapira, Yoav" <>
Subject RE: HttpSession instance is differenct for each new request!
Date Wed, 09 Apr 2003 13:10:56 GMT


>Until recently we used the session object in order to synchronize some
>blocks on:
>synchronized ( request.getSession() ) { ... }

Not the best design pattern.  Did you get this from a book or other

>Yesterday I found out that each time you call request.getSession(),
>tomcat/catalina returns another instance of SessionFacade. Each


>instance represents the same session (getAttribute and getId return
>the same values each time) but still it is very unfortunate and
>questionable that a different object is returned each time (causing
>synchronization on the session object to break).

You shouldn't synchronize on the session object itself, but on one of
its attribute, as I see you're already doing.  The servlet specification
does not guarantee you the same session instance (consider clustered
environments, for a counter-example), only the same ID (and attributes

So tomcat is consistent with the spec.  Your above synchronization
pattern relies on behavior not in the servlet-spec.  

>Is this normal, should not the same object be returned every time? Has
>it maybe been fixed in newer tomcat versions (we are still using

It hasn't been fixed, it's not a bug.  It's "normal" in so far as spec

>Now I have to go through great lengths in order to create a unique
>Object as session-attribute to synchronzie on instead:
>synchronized ( request.getSession().getAttribute("theSyncObject") )
>Initially creating this attribute needs to be synchronized on the
>session as well, creating a chicken and egg problem :) Now I was
>forced to synchronize this step on the application (on some basic
>Class object).

I don't think that's particularly great lengths, but I'm not clear on
why you need to synchronize on a session attribute in the first place.
If you could give more details about your app's flow and what you're
trying to do, maybe there's an easier way to do it.

Maybe a Singleton could keep theSyncObject?  That would be pretty easy.

Yoav Shapira
Millennium ChemInformatics

This e-mail, including any attachments, is a confidential business communication, and may
contain information that is confidential, proprietary and/or privileged.  This e-mail is intended
only for the individual(s) to whom it is addressed, and may not be saved, copied, printed,
disclosed or used by anyone else.  If you are not the(an) intended recipient, please immediately
delete this e-mail from your computer system and notify the sender.  Thank you.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message