geronimo-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Quintin Beukes <>
Subject Re: Stateful session bean has no state
Date Mon, 12 Oct 2009 20:14:57 GMT
Just to clarify.

An EJB is referenced through a "proxy". When you get the instance to
the session bean, you actually obtain a reference to a proxy class
which implements the EJB's business interface (the one annotated with
@Local or @Remote). So any methods you invoke are done so on the
proxy, and the proxy does the work of communicating with the actual
EJB instance.

So, with a Stateless bean, every invocation on the obtained instance
(ie. the proxy), can happen on a different physical instance. So it's
at the whim of the implementation/provider, and therefore you can
never rely on any member values being the same in between method
calls. Literally, this code could work with 2 separate physical
instances, or it could work with one - you can never be sure - it's a
stateless EJB.
public void someEjbInvocation();
  MyStatelessEjbLocal myEjb = initialContext.lookup("MyStatelessEjbLocal");

With a Stateful bean, when you get a reference via the InitialContext
or injection, it will still be via a proxy as above, but this time it
will always be to the same physical bean instance. So any method
invocations like in the example above will be guaranteed to happen on
the same object instance, and therefore you can store values in it's
member fields and know that it will be there next time. Everytime you
do a lookup to the Stateful bean via the InitialContext, you will get
a reference to a proxy pointing to a new/fresh EJB instance of that
bean. So the following will be 2 separate instances of the same
stateful EJB:
public void someEjbInvocation();
  MyStatefulEjbLocal myEjbInstance1 =

  MyStatefulEjbLocal myEjbInstance2 =

So if you want to do your invocations on the same instance, ie. keep
the state, you need to be sure that you keep the reference somewhere,
like in the session object (HttpSession) of the servlet/JSP. Otherwise
you will keep getting a new instance and you're state won't be saved.

Just a tip: Further, when you're done with the session bean (like when
the client logs out), call it's remove method (any empty method on the
bean annotated with @javax.ejb.Remove - it must be exposed via the
interfaces as well). This will cause the bean to be garbage collected.
The alternative is to have it timeout, but in a very busy system with
long timeouts this could potentially waste a lot of resources.

I hope this solves your problem.

Quintin Beukes

On Mon, Oct 12, 2009 at 9:50 PM, Quintin Beukes <> wrote:
> Are you storing your session bean instance in the servlet/JSP session context?
> Quintin Beukes
> On Mon, Oct 12, 2009 at 8:48 PM, Antonio Fornié
> <> wrote:
>> Hello again!
>> I made a stateful sessiob bean and it's working the same way my stateless
>> beans do: they forget their state. Here's my code:
>> @Stateful
>> public class CartBean implements CartLocal {
>>        private Integer number = new Integer(0);
>>        public void setNumber(Integer number) {
>>                this.number = number;
>>        }
>>        public Integer getNumber() {
>>                return this.number;
>>        }
>>        @Override
>>        public Integer getNumberService() {
>>                this.number += 10;
>>                return this.productNumber;
>>        }
>> }
>> My jsp uses CartLocal interface wich only shows "getNumberService()" method.
>> When requesting the jsp with my web browser it always shows ten. when
>> refreshing the page it should show 20, but once again shows 10. What am I
>> doing wrong?
>> Thank you very much!
>> --
>> View this message in context:
>> Sent from the Apache Geronimo - Users mailing list archive at

View raw message