Life Cycle of Stateful and Stateless Session Beans.

Alessandro A. Garbagnati

Stateful Session Bean
A stateless session bean has only two states: Does Not Exists and Method Ready Pool.
A bean has not yet instantiated (so it is not an instance in memory) when it is in the Does Not Exists state.
When the EJB container needs one or more beans, it creates and set them in the Method Ready Pool state. This happens through the creation of a new instance (Class.newInstance()), then it is set its context (setSessionContext()) and finally calls the ejbCreate() method.
The ejbRemove() method is called to move a bean from the Method Ready Pool back to Does Not Exists state.

Stateful Session Bean
Unlike Stateless, a Stateful Session Bean has three states. Does not exists, Method Ready and Passivated states.
Like Stateless beans, when the Stateful Session Bean hasn't been instantiated yet (so it is not an instance in memory) is it in the Does not exists state.
Once a container creates one or more instances of a Stateful Session Bean it sets them in a Method Ready state. In this state it can serve requests from its clients. Like Stateless Session Beans, a new instance is created (Class.newInstance()), the context is passed (setSessionContext()) and finally the bean is created with ejbCreate().
During the life of a Stateful Session Bean, there are periods of inactivity. In these periods, the container can set the bean to the Passivate state. This happens through the ejbPassivate() method. From the Passivate state the bean can be moved back to the Method Ready state, via ejbActivate() method, or can go directly to the Does Not Exists state with ejbRemove().