[prev in list] [next in list] [prev in thread] [next in thread] 

List:       struts-user
Subject:    Re: Struts2 session concurrency issue ?
From:       Dionis Argiri <diostm () gmail ! com>
Date:       2012-04-26 5:12:25
Message-ID: CAFrzSN5NYRkQNfDHq0v9hADFR90-kNiJ_56hE=kKB4QkrhZS2g () mail ! gmail ! com
[Download RAW message or body]


Actual problem was with my load testing utility. I have not used jsessionid
and thus it was causing the "problem". Also I have took a look at source
code. When calling ActionContext.getContext().getSession() -
SessionMap<String, Object> is returned. It is a map, that wraps session and
uses synchronization on it. Thus it's a thread safe realisation. Anyway,
thank you for the support.

26 апреля 2012 г. 1:32 пользователь Rene Gielen
<rene.gielen@googlemail.com>написал:

> Please remember that the HTTP session is not synchronized. Thus, using the
> session.put is a critical method. Most likely, what you really want to do
> is
> counter = session.get("counter");
> counter.increment (or incrementAndGet)
> In this way you ensure exactly the same object is accessed and incremted /
> consumed.
>
> On Wed, Apr 25, 2012 at 4:03 PM, Gabriel Belingueres
> <belingueres@gmail.com>wrote:
>
> > The "S2 way" of accessing the session is implementing the SessionAware
> > interface.
> >
> > First you must realize that what you are putting on session scope is
> > not the AtomicInteger per se, but its current value at the moment you
> > call the first action, so unless you call the first action again, the
> > value stored in session will NOT change.
> > The second action does not change the value in session, just print
> > what was already stored.
> >
> > When you said that after some time, the results are "stabilized", I
> > guess that you are requesting the first action after no other request
> > is running so you get the current value again and then call the second
> > action serially, giving you the same value.
> >
> > Also, you have concurrency issues if the first action:
> >
> > HitCounter.increment();
> > ActionContext.getContext().getSession().put("counter",
> > HitCounter.getAtomicCounter());
> >
> > you increment without returning the new value. Instead you call the
> > getAtomicCounter() method, which will cause interference between
> > threads (ie not thread safe), if the point that every session have a
> > different counter value, then you must change the increment method
> > like this:
> >   public static int increment() {
> >       return counter.incrementAndGet();
> >   }
> > and use that value to store it in session (no call to
> getAtomicCounter()).
> >
> > HTH,
> > Gabriel
> >
> > 2012/4/25 Dionis Argiri <diostm@gmail.com>:
> > > Hi!
> > >
> > > I have some troubles (with understanding?) how session works.
> > >
> > > For example, I have two actions: First and Second.
> > >
> > > I have written simple utility, that uses executor service to send
> 100000
> > > asynchronous requests to action Second.
> > >
> > > In First action (which is called only from my browser) I do:
> > >
> > > HitCounter.increment();
> > > ActionContext.getContext().getSession().put("counter",
> > > HitCounter.getAtomicCounter());
> > > return Action.SUCCESS;
> > >
> > > In second action I do:
> > >
> > > System.out.println("From session:
> > > "+ActionContext.getContext().getSession().get("counter"));
> > > System.out.println("Actual:"+ HitCounter.getAtomicCounter());
> > > return Action.SUCCESS;
> > >
> > > And the output I see(and it really makes me mad):
> > >
> > > From session: 2
> > > Actual: 69352
> > >
> > > After some time when I use this Fitst action/Second action only from my
> > > browsers and no concurrent requests come(generated by my load utility),
> > > results "are stabilized" to actual values. Thus, it seems that I have
> > > concurrency issues with session or I'm missing some points.
> > >
> > > Is there a standard way that I should use to solve this problem, when
> > using
> > > Struts2 ?
> > >
> > > P.S. HitCounter realisation:
> > >
> > > public class HitCounter {
> > >    private static AtomicInteger counter = new AtomicInteger(0);
> > >
> > >    public static void increment() {
> > >        counter.incrementAndGet();
> > >    }
> > >    public static int getAtomicCounter() {
> > >        return counter.get();
> > >    }
> > > }
> > >
> > > BR,
> > >
> > > Dionis
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
>


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic