Quoting "Sergei P. Volin" <[EMAIL PROTECTED]>:

> 
> Greetings,
> 
> That is the question! Today morning I've found the reason of my troubles. It
> was my negligence in how I assigned a value to the iterated list that I
> placed in the object which I then stored in a session for JSP's iterator. I
> declared the list in my action as a global variable, assigned a value to it
> in the protected void method which is called by the action execute method
> like this:
> 
> public class MyAction  extends Action {
>     protected ArrayList list = new ArrayList();
> ...
>     public ActionForward execute(...) ... {
>         list = new ArrayList();
>         myMethod(...);
>         obj.setList(list)
>         session.setAttribute("buinessObj", obj);
>         ...
>         return mapping.findForward(forward);
>     }
>     protected void myMethod(...) {
>         ...
>         while (rs.next()) {
>         ...
>             list.add(el);
>         ...
>         }
>     }
> }
> 
> Now this piece of code looks like:
> 
> public class MyAction  extends Action {
>     protected ArrayList list = new ArrayList();
> ...
>     public ActionForward execute(...) ... {
>         list = new ArrayList();
>         myMethod(...);
>         obj.setList(list)
>         session.setAttribute("buinessObj", obj);
>         ...
>         return mapping.findForward(forward);
>     }
>     protected void myMethod(...) {
>         ArrayList list = new ArrayList();        // new
>         ...
>         while (rs.next()) {
>         ...
>             list.add(el);
>         ...
>         }
>         this.list =list;                        // new
>     }
> }
> 
> It looks that with this code I rid off all my troubles now - no
> ConcurrentModificationException, no doubling of list size. I made a dozen of
> tests - no exceptions and the results were correct.
> 
> Mr. Craig R. McClanahan! Am I right now with my code? Please, reply.
> Thanks a lot,

No, you are still going to have a problem with the shared instance variable when
two requests occur at the same time.  A general principle you should follow is
to *never* use instance variables in an Action (or a servlet, for that matter)
to store anything that relates only to the current request.  It's fine to use
instance variables to share things that are common to all requests, though.

An easy way to do this in your scenario would be something like this:

 public class MyAction  extends Action {
     // protected ArrayList list = new ArrayList(); (COMMENTED OUT)
 ...
     public ActionForward execute(...) ... {
         List list = myMethod(); // NEWER
         // myMethod(...); (COMMENTED OUT)
         obj.setList(list)
         session.setAttribute("buinessObj", obj);
         ...
         return mapping.findForward(forward);
     }
     // protected void myMethod(...) { (COMMENTED OUT)
     protected List myMethod(...) {  // NEWER
         ArrayList list = new ArrayList();        // new
         ...
         while (rs.next()) {
         ...
             list.add(el);
         ...
         }
         // this.list =list;                        // new (COMMENTED OUT)
         return list;
     }
 }


Craig


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

Reply via email to