You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by David Graham <dg...@hotmail.com> on 2003/01/27 23:50:57 UTC

[OT] Synchronizing Session Objects

I haven't found a definitive answer to whether you should synchronize access 
to objects stored in the session scope.  Do you have to put a synchronized 
block around session objects when iterating over them or modifying them?  
I'm guessing that if you treat session or application scoped objects as read 
only you don't have to synchronize.

Thanks,
David





_________________________________________________________________
MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.  
http://join.msn.com/?page=features/virus


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [OT] Synchronizing Session Objects

Posted by Mark Galbreath <ma...@qat.com>.
I actually taught myself Scheme (the MIT version) on the Macintosh in the
early 90s....  :-)

-----Original Message-----
From: James Turner [mailto:turner@blackbear.com] 
Sent: Tuesday, January 28, 2003 10:35 AM
> 
> LISP boy is correct, and I brought this subject up about a
> year ago and Craig answered pretty definitively - try a 
> search on "synchronization" in the archive:

LISP Boy?  Do I get the power to warp men's minds using my LISPParens? Can I
self-modify code in a single bound?  Does this make me the sidekick of
SchemeMan?

Holy PDL Stack Overflow, SchemeMan!



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [OT] Synchronizing Session Objects

Posted by James Turner <tu...@blackbear.com>.
> From: Mark Galbreath [mailto:mark_galbreath@qat.com] 
> Sent: Tuesday, January 28, 2003 9:14 AM
> To: 'Struts Users Mailing List'
> Subject: RE: [OT] Synchronizing Session Objects
> 
> 
> LISP boy is correct, and I brought this subject up about a 
> year ago and Craig answered pretty definitively - try a 
> search on "synchronization" in the archive:

LISP Boy?  Do I get the power to warp men's minds using my LISPParens?
Can I self-modify code in a single bound?  Does this make me the
sidekick of SchemeMan?

Holy PDL Stack Overflow, SchemeMan!

James Turner
Owner & Manager, Black Bear Software, LLC
turner@blackbear.com

Author: 
    MySQL & JSP Web Applications: 
        Data Driven Programming Using Tomcat and MySQL
    ISBN 0672323095; Sams, 2002

Co-Author: 
    Struts Kick Start
    ISBN 0672324725; Sams, 2002

Forthcoming:
    JavaServer Faces Kick Start 
    Sams, Fall 2003




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [OT] Synchronizing Session Objects

Posted by Mark Galbreath <ma...@qat.com>.
LISP boy is correct, and I brought this subject up about a year ago and
Craig answered pretty definitively - try a search on "synchronization" in
the archive:

http://www.mail-archive.com/struts-user%40jakarta.apache.org/

Mark

-----Original Message-----
From: James Turner [mailto:turner@blackbear.com] 
Sent: Monday, January 27, 2003 7:30 PM
To: 'Struts Users Mailing List'
Subject: RE: [OT] Synchronizing Session Objects


> From: David Graham [mailto:dgraham1980@hotmail.com]
> Sent: Monday, January 27, 2003 5:51 PM
> To: struts-user@jakarta.apache.org
> Subject: [OT] Synchronizing Session Objects
> 
> 
> I haven't found a definitive answer to whether you should
> synchronize access 
> to objects stored in the session scope.  Do you have to put a 
> synchronized 
> block around session objects when iterating over them or 
> modifying them?  
> I'm guessing that if you treat session or application scoped 
> objects as read 
> only you don't have to synchronize.

Frankly, I wouldn't obsess about session scoped objects either.  In order
for a thread issue to intrude, they'd have to have two requests running
against the same session at the same time, which can happen (submit, back,
submit), but is uncommon.


James Turner
Owner & Manager, Black Bear Software, LLC
turner@blackbear.com

Author: 
    MySQL & JSP Web Applications: 
        Data Driven Programming Using Tomcat and MySQL
    ISBN 0672323095; Sams, 2002

Co-Author: 
    Struts Kick Start
    ISBN 0672324725; Sams, 2002

Forthcoming:
    JavaServer Faces Kick Start 
    Sams, Fall 2003


> -----Original Message-----
> 
> 
> 
> 
> 
> _________________________________________________________________
> MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.
> http://join.msn.com/?page=features/virus
> 
> 
> --
> To unsubscribe, e-mail:   
> <mailto:struts-user-> unsubscribe@jakarta.apache.org>
> For
> additional commands, 
> e-mail: <ma...@jakarta.apache.org>
> 



--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [OT] Synchronizing Session Objects

Posted by Andrew Hill <an...@gridnode.com>.
Your forgetting the ability of users to open links in new windows. Your user
could have several windows open using the same session. Ditto for frames.

-----Original Message-----
From: James Turner [mailto:turner@blackbear.com]
Sent: Tuesday, 28 January 2003 08:30
To: 'Struts Users Mailing List'
Subject: RE: [OT] Synchronizing Session Objects


> From: David Graham [mailto:dgraham1980@hotmail.com]
> Sent: Monday, January 27, 2003 5:51 PM
> To: struts-user@jakarta.apache.org
> Subject: [OT] Synchronizing Session Objects
>
>
> I haven't found a definitive answer to whether you should
> synchronize access
> to objects stored in the session scope.  Do you have to put a
> synchronized
> block around session objects when iterating over them or
> modifying them?
> I'm guessing that if you treat session or application scoped
> objects as read
> only you don't have to synchronize.

Frankly, I wouldn't obsess about session scoped objects either.  In
order for a thread issue to intrude, they'd have to have two requests
running against the same session at the same time, which can happen
(submit, back, submit), but is uncommon.


James Turner
Owner & Manager, Black Bear Software, LLC
turner@blackbear.com

Author:
    MySQL & JSP Web Applications:
        Data Driven Programming Using Tomcat and MySQL
    ISBN 0672323095; Sams, 2002

Co-Author:
    Struts Kick Start
    ISBN 0672324725; Sams, 2002

Forthcoming:
    JavaServer Faces Kick Start
    Sams, Fall 2003


> -----Original Message-----
>
>
>
>
>
> _________________________________________________________________
> MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.
> http://join.msn.com/?page=features/virus
>
>
> --
> To unsubscribe, e-mail:
> <mailto:struts-user-> unsubscribe@jakarta.apache.org>
> For
> additional commands,
> e-mail: <ma...@jakarta.apache.org>
>



--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [OT] Synchronizing Session Objects

Posted by James Turner <tu...@blackbear.com>.
> From: David Graham [mailto:dgraham1980@hotmail.com] 
> Sent: Monday, January 27, 2003 5:51 PM
> To: struts-user@jakarta.apache.org
> Subject: [OT] Synchronizing Session Objects
> 
> 
> I haven't found a definitive answer to whether you should 
> synchronize access 
> to objects stored in the session scope.  Do you have to put a 
> synchronized 
> block around session objects when iterating over them or 
> modifying them?  
> I'm guessing that if you treat session or application scoped 
> objects as read 
> only you don't have to synchronize.

Frankly, I wouldn't obsess about session scoped objects either.  In
order for a thread issue to intrude, they'd have to have two requests
running against the same session at the same time, which can happen
(submit, back, submit), but is uncommon.


James Turner
Owner & Manager, Black Bear Software, LLC
turner@blackbear.com

Author: 
    MySQL & JSP Web Applications: 
        Data Driven Programming Using Tomcat and MySQL
    ISBN 0672323095; Sams, 2002

Co-Author: 
    Struts Kick Start
    ISBN 0672324725; Sams, 2002

Forthcoming:
    JavaServer Faces Kick Start 
    Sams, Fall 2003


> -----Original Message-----
> 
> 
> 
> 
> 
> _________________________________________________________________
> MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.  
> http://join.msn.com/?page=features/virus
> 
> 
> --
> To unsubscribe, e-mail:   
> <mailto:struts-user-> unsubscribe@jakarta.apache.org>
> For 
> additional commands, 
> e-mail: <ma...@jakarta.apache.org>
> 



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [OT] Synchronizing Session Objects

Posted by Andrew Hill <an...@gridnode.com>.
hehe my actions take a lot longer than a split second...

-----Original Message-----
From: Mark Galbreath [mailto:mark_galbreath@qat.com]
Sent: Tuesday, 28 January 2003 22:43
To: 'Struts Users Mailing List'
Subject: RE: [OT] Synchronizing Session Objects


Yeah, that happens all the time....

-----Original Message-----
From: news [mailto:news@main.gmane.org] On Behalf Of V. Cekvenich
Sent: Monday, January 27, 2003 9:35 PM
To: struts-user@jakarta.apache.org
Subject: Re: [OT] Synchronizing Session Objects


A users opens another browser session and your action takes a bit longer
than a split second.
.V

James Turner wrote:
>>From: Craig R. McClanahan [mailto:craigmcc@apache.org]
>>Sent: Monday, January 27, 2003 8:57 PM
>>To: Struts Users Mailing List
>>Subject: Re: [OT] Synchronizing Session Objects
>>
>>If you're modifying them, you definitely should -- there are
>>lots of cases where it is possible to have multiple
>>simultaneous requests for the same session.
>>
>
>
> I'd be curious what the non-trivial cases of this would be.  You can
> certainly get multiple simultaneous requests if you have images
> loading on a page, or that type of stuff.  But where would you (for
> example) see it being possible for multiple Struts Actions to be
> running at once on the same session, without it being some kind of
> "the user hit the back button" scenario?  I can see this happening in
> pure servlet code, where a give page might be calling several servlets
> at once, but isn't that the benefit of MVC, that all the stuff that
> would be likely to need to be synchronized happens in a single method
> (the Action), and the page merely displays results?
>
> James Turner
> Owner & Manager, Black Bear Software, LLC turner@blackbear.com
>
> Author:
>     MySQL & JSP Web Applications:
>         Data Driven Programming Using Tomcat and MySQL
>     ISBN 0672323095; Sams, 2002
>
> Co-Author:
>     Struts Kick Start
>     ISBN 0672324725; Sams, 2002
>
> Forthcoming:
>     JavaServer Faces Kick Start
>     Sams, Fall 2003



--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>




--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [OT] Synchronizing Session Objects

Posted by Mark Galbreath <ma...@qat.com>.
Yeah, that happens all the time....

-----Original Message-----
From: news [mailto:news@main.gmane.org] On Behalf Of V. Cekvenich
Sent: Monday, January 27, 2003 9:35 PM
To: struts-user@jakarta.apache.org
Subject: Re: [OT] Synchronizing Session Objects


A users opens another browser session and your action takes a bit longer 
than a split second.
.V

James Turner wrote:
>>From: Craig R. McClanahan [mailto:craigmcc@apache.org]
>>Sent: Monday, January 27, 2003 8:57 PM
>>To: Struts Users Mailing List
>>Subject: Re: [OT] Synchronizing Session Objects
>>
>>If you're modifying them, you definitely should -- there are
>>lots of cases where it is possible to have multiple 
>>simultaneous requests for the same session.
>>
> 
> 
> I'd be curious what the non-trivial cases of this would be.  You can 
> certainly get multiple simultaneous requests if you have images 
> loading on a page, or that type of stuff.  But where would you (for 
> example) see it being possible for multiple Struts Actions to be 
> running at once on the same session, without it being some kind of 
> "the user hit the back button" scenario?  I can see this happening in 
> pure servlet code, where a give page might be calling several servlets 
> at once, but isn't that the benefit of MVC, that all the stuff that 
> would be likely to need to be synchronized happens in a single method 
> (the Action), and the page merely displays results?
> 
> James Turner
> Owner & Manager, Black Bear Software, LLC turner@blackbear.com
> 
> Author: 
>     MySQL & JSP Web Applications: 
>         Data Driven Programming Using Tomcat and MySQL
>     ISBN 0672323095; Sams, 2002
> 
> Co-Author: 
>     Struts Kick Start
>     ISBN 0672324725; Sams, 2002
> 
> Forthcoming:
>     JavaServer Faces Kick Start 
>     Sams, Fall 2003



--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [OT] Synchronizing Session Objects

Posted by "V. Cekvenich" <vc...@basebeans.com>.
A users opens another browser session and your action takes a bit longer 
than a split second.
.V

James Turner wrote:
>>From: Craig R. McClanahan [mailto:craigmcc@apache.org] 
>>Sent: Monday, January 27, 2003 8:57 PM
>>To: Struts Users Mailing List
>>Subject: Re: [OT] Synchronizing Session Objects
>>
>>If you're modifying them, you definitely should -- there are 
>>lots of cases where it is possible to have multiple 
>>simultaneous requests for the same session.
>>
> 
> 
> I'd be curious what the non-trivial cases of this would be.  You can
> certainly get multiple simultaneous requests if you have images loading
> on a page, or that type of stuff.  But where would you (for example) see
> it being possible for multiple Struts Actions to be running at once on
> the same session, without it being some kind of "the user hit the back
> button" scenario?  I can see this happening in pure servlet code, where
> a give page might be calling several servlets at once, but isn't that
> the benefit of MVC, that all the stuff that would be likely to need to
> be synchronized happens in a single method (the Action), and the page
> merely displays results?
> 
> James Turner
> Owner & Manager, Black Bear Software, LLC
> turner@blackbear.com
> 
> Author: 
>     MySQL & JSP Web Applications: 
>         Data Driven Programming Using Tomcat and MySQL
>     ISBN 0672323095; Sams, 2002
> 
> Co-Author: 
>     Struts Kick Start
>     ISBN 0672324725; Sams, 2002
> 
> Forthcoming:
>     JavaServer Faces Kick Start 
>     Sams, Fall 2003



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [OT] Synchronizing Session Objects

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Mon, 27 Jan 2003, James Turner wrote:

> Date: Mon, 27 Jan 2003 21:16:05 -0500
> From: James Turner <tu...@blackbear.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: 'Struts Users Mailing List' <st...@jakarta.apache.org>
> Subject: RE: [OT] Synchronizing Session Objects
>
> > From: Craig R. McClanahan [mailto:craigmcc@apache.org]
> > Sent: Monday, January 27, 2003 8:57 PM
> > To: Struts Users Mailing List
> > Subject: Re: [OT] Synchronizing Session Objects
> >
> > If you're modifying them, you definitely should -- there are
> > lots of cases where it is possible to have multiple
> > simultaneous requests for the same session.
> >
>
> I'd be curious what the non-trivial cases of this would be.  You can
> certainly get multiple simultaneous requests if you have images loading
> on a page, or that type of stuff.  But where would you (for example) see
> it being possible for multiple Struts Actions to be running at once on
> the same session, without it being some kind of "the user hit the back
> button" scenario?  I can see this happening in pure servlet code, where
> a give page might be calling several servlets at once, but isn't that
> the benefit of MVC, that all the stuff that would be likely to need to
> be synchronized happens in a single method (the Action), and the page
> merely displays results?
>

Having a controller doesn't avoid the need to synchronize things that get
modified -- consider a shopping cart implemented as an ArrayList
maintained in the session, and a JSP page that uses <logic:iterate> to
display it.  If the List is modified during the JSP's iteration, the page
is going to throw a very ugly ConcurrentModificationException.

By the way, here's two trivially simple ways to cause this problem:

(1) Use frames where the pages in each frame reference the same
collection, and at least one of the frames modifies it.

(2) Have users that submit a request, press STOP, and submit another
request.  (You're just as likely to end up trying to do simultaneous
modifications in this scenario, which is even worse than a modification
plus iteration scenario.)

To say nothing of application scenarios where you've cached data in the
session that is being modified by some sort of background process, which
is not unusal in sophisticated applications.

> James Turner

Craig


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: [OT] Synchronizing Session Objects

Posted by James Turner <tu...@blackbear.com>.
> From: Craig R. McClanahan [mailto:craigmcc@apache.org] 
> Sent: Monday, January 27, 2003 8:57 PM
> To: Struts Users Mailing List
> Subject: Re: [OT] Synchronizing Session Objects
>
> If you're modifying them, you definitely should -- there are 
> lots of cases where it is possible to have multiple 
> simultaneous requests for the same session.
> 

I'd be curious what the non-trivial cases of this would be.  You can
certainly get multiple simultaneous requests if you have images loading
on a page, or that type of stuff.  But where would you (for example) see
it being possible for multiple Struts Actions to be running at once on
the same session, without it being some kind of "the user hit the back
button" scenario?  I can see this happening in pure servlet code, where
a give page might be calling several servlets at once, but isn't that
the benefit of MVC, that all the stuff that would be likely to need to
be synchronized happens in a single method (the Action), and the page
merely displays results?

James Turner
Owner & Manager, Black Bear Software, LLC
turner@blackbear.com

Author: 
    MySQL & JSP Web Applications: 
        Data Driven Programming Using Tomcat and MySQL
    ISBN 0672323095; Sams, 2002

Co-Author: 
    Struts Kick Start
    ISBN 0672324725; Sams, 2002

Forthcoming:
    JavaServer Faces Kick Start 
    Sams, Fall 2003




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [OT] Synchronizing Session Objects

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Mon, 27 Jan 2003, David Graham wrote:

> Date: Mon, 27 Jan 2003 15:50:57 -0700
> From: David Graham <dg...@hotmail.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: struts-user@jakarta.apache.org
> Subject: [OT] Synchronizing Session Objects
>
> I haven't found a definitive answer to whether you should synchronize access
> to objects stored in the session scope.  Do you have to put a synchronized
> block around session objects when iterating over them or modifying them?

If you're modifying them, you definitely should -- there are lots of cases
where it is possible to have multiple simultaneous requests for the same
session.

Note that it is *not* the session.getAttribute(), session.setAttribute(),
or session.removeAttribute() call that needs to be synchronized -- the
container takes care of that for you.

> I'm guessing that if you treat session or application scoped objects as read
> only you don't have to synchronize.
>
> Thanks,
> David
>

Craig



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>