On 18 Dec 2002, Felipe Schnack wrote:

> Date: 18 Dec 2002 09:12:20 -0200
> From: Felipe Schnack <[EMAIL PROTECTED]>
> Reply-To: Tomcat Users List <[EMAIL PROTECTED]>
> To: Tomcat Users List <[EMAIL PROTECTED]>
> Subject: Re: HttpSession issues
>
>   Yes, but this is related to an user session. How is possible to have
> concurrent updates on an user session when an user have only one browser
> window open and does a refresh?

Here's at least two very simple ways:

* Use frames (the requests for each frame will happen
  simultaneously yet belong to the same session)

* User presses REFRESH before the previous request
  has completed.  (Same thing could happen if you're
  using a meta refresh tag to automatically refresh).

In either case, your session will be processed by more than one request at
the same time (on different request processing threads), so anything you
store in the session needs to be thread safe.

> This iteration is done in a taglib. And
> why this only happens when he refreshes the pages, but not when he hits
> enter in the url bar?
>   BTW, inside this loop in some circumstances I change the session
> attributes. In other words, the code look like this:
>
>               Enumeration attrs = session.getAttributeNames();
>               while(attrs.hasMoreElements())
>               {
>                       String name = (String)attrs.nextElement();
>                       Object value = session.getAttribute(name);
>                       if (<some weird conditions>)
>                       {
>                               session.removeAttribute(name);
>                       }
>               }
>
>   This removeAttribute() is the problem?
>

Yes.  And it's not a thread safety issue.

You are modifying the collection (i.e. the internal HashMap of session
attributes) that you are enumerating over.  Such behavior does not need to
be supported by a java collections class.

The safe way to do this is accumulate a separate list of just the session
keys you want to remove, then run a separate iteration over those keys and
call session.removeAttribute().

Craig


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to