You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Joe Hertz <jh...@patriot.net> on 2003/12/18 08:56:28 UTC

Are httpSessions thread safe?

Not sure how OT this question is.

My current plan (unless this is bad for some reason, but if so, Ted H
should change his example app :-) is to stash the hibernate Session for
a user into his httpSession, and reuse it on each request.

A Hibernate Session instance isn't threadsafe. I imagine if two really
quick http requests got generated out of the same browser, all hell
could break out.

I guess I want to know if mortals like me need to worry about this.

Does Struts (or the Servlet container FAIK) prevent this from occuring,
or do I need to ensure this doesn't happen? If so, how? With a token or
is there a better strategy?

TIA

-Joe



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Are httpSessions thread safe?

Posted by Max Cooper <ma...@maxcooper.com>.
Two threads can access and use the same object from the session at the same
time. Struts does not prevent this from occurring.

It is something to worry about unless you want to be woken up with bug
reports in the middle of the night. :-)

-Max

----- Original Message ----- 
From: "Joe Hertz" <jh...@patriot.net>
To: "'Struts Users Mailing List'" <st...@jakarta.apache.org>
Sent: Wednesday, December 17, 2003 11:56 PM
Subject: Are httpSessions thread safe?


> Not sure how OT this question is.
>
> My current plan (unless this is bad for some reason, but if so, Ted H
> should change his example app :-) is to stash the hibernate Session for
> a user into his httpSession, and reuse it on each request.
>
> A Hibernate Session instance isn't threadsafe. I imagine if two really
> quick http requests got generated out of the same browser, all hell
> could break out.
>
> I guess I want to know if mortals like me need to worry about this.
>
> Does Struts (or the Servlet container FAIK) prevent this from occuring,
> or do I need to ensure this doesn't happen? If so, how? With a token or
> is there a better strategy?
>
> TIA
>
> -Joe
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: Are httpSessions thread safe?

Posted by Kris Schneider <kr...@dotech.com>.
If Hibernate sessions are not thread-safe by design, then stuffing one in a web
app session and wrapping it with your own synchronization doesn't seem like the
right approach. I've never actually used Hibernate, so I could be way off base,
but it sounds more like a job for a filter. The filter would initialize the
Hibernate session and store it as a request attribute before any other request
processing. It would then also tear down the Hibernate session at the end of the
request. Are Hibernate sessions expensive enough resources to warrant creating
only one per web app session? Another approach might be to set up Hibernate
sessions as thread-local variable...

Quoting Andrew Hill <an...@gridnode.com>:

> The sessions essentially just a sort of Map. Access to it may be
> threadsafe,
> but the stuff thats in it is another matter entirely. Multiple requests
> associated with the same session will execute simultaneously.
> 
> If you have >1 threads playing with the same unsafe object (which you just
> happened to pull out of the session) then of course you will need to
> synchronise their access to it externally.
> 
> ie:
> 
> Garthop unsafeObject = (Garthop)session.getAttribute(THE_GARTHOP);
> synchronized(unsafeObject)
> {
>   garthop.nargle();
> }
> 
> 
> 
> 
> -----Original Message-----
> From: Joe Hertz [mailto:jhertz@patriot.net]
> Sent: Thursday, 18 December 2003 15:56
> To: 'Struts Users Mailing List'
> Subject: Are httpSessions thread safe?
> 
> 
> Not sure how OT this question is.
> 
> My current plan (unless this is bad for some reason, but if so, Ted H
> should change his example app :-) is to stash the hibernate Session for
> a user into his httpSession, and reuse it on each request.
> 
> A Hibernate Session instance isn't threadsafe. I imagine if two really
> quick http requests got generated out of the same browser, all hell
> could break out.
> 
> I guess I want to know if mortals like me need to worry about this.
> 
> Does Struts (or the Servlet container FAIK) prevent this from occuring,
> or do I need to ensure this doesn't happen? If so, how? With a token or
> is there a better strategy?
> 
> TIA
> 
> -Joe

-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Are httpSessions thread safe?

Posted by David Erickson <de...@cmcflex.com>.
Another question along the same vein.. each httpRequest that comes into say
Tomcat is given a seperate thread to operate under correct?
-David

----- Original Message ----- 
From: "Andrew Hill" <an...@gridnode.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Thursday, December 18, 2003 1:09 AM
Subject: RE: Are httpSessions thread safe?


> The sessions essentially just a sort of Map. Access to it may be
threadsafe,
> but the stuff thats in it is another matter entirely. Multiple requests
> associated with the same session will execute simultaneously.
>
> If you have >1 threads playing with the same unsafe object (which you just
> happened to pull out of the session) then of course you will need to
> synchronise their access to it externally.
>
> ie:
>
> Garthop unsafeObject = (Garthop)session.getAttribute(THE_GARTHOP);
> synchronized(unsafeObject)
> {
>   garthop.nargle();
> }
>
>
>
>
> -----Original Message-----
> From: Joe Hertz [mailto:jhertz@patriot.net]
> Sent: Thursday, 18 December 2003 15:56
> To: 'Struts Users Mailing List'
> Subject: Are httpSessions thread safe?
>
>
> Not sure how OT this question is.
>
> My current plan (unless this is bad for some reason, but if so, Ted H
> should change his example app :-) is to stash the hibernate Session for
> a user into his httpSession, and reuse it on each request.
>
> A Hibernate Session instance isn't threadsafe. I imagine if two really
> quick http requests got generated out of the same browser, all hell
> could break out.
>
> I guess I want to know if mortals like me need to worry about this.
>
> Does Struts (or the Servlet container FAIK) prevent this from occuring,
> or do I need to ensure this doesn't happen? If so, how? With a token or
> is there a better strategy?
>
> TIA
>
> -Joe
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: Are httpSessions thread safe?

Posted by Andrew Hill <an...@gridnode.com>.
The sessions essentially just a sort of Map. Access to it may be threadsafe,
but the stuff thats in it is another matter entirely. Multiple requests
associated with the same session will execute simultaneously.

If you have >1 threads playing with the same unsafe object (which you just
happened to pull out of the session) then of course you will need to
synchronise their access to it externally.

ie:

Garthop unsafeObject = (Garthop)session.getAttribute(THE_GARTHOP);
synchronized(unsafeObject)
{
  garthop.nargle();
}




-----Original Message-----
From: Joe Hertz [mailto:jhertz@patriot.net]
Sent: Thursday, 18 December 2003 15:56
To: 'Struts Users Mailing List'
Subject: Are httpSessions thread safe?


Not sure how OT this question is.

My current plan (unless this is bad for some reason, but if so, Ted H
should change his example app :-) is to stash the hibernate Session for
a user into his httpSession, and reuse it on each request.

A Hibernate Session instance isn't threadsafe. I imagine if two really
quick http requests got generated out of the same browser, all hell
could break out.

I guess I want to know if mortals like me need to worry about this.

Does Struts (or the Servlet container FAIK) prevent this from occuring,
or do I need to ensure this doesn't happen? If so, how? With a token or
is there a better strategy?

TIA

-Joe



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org