SCBCD Notes: Stateful Session Bean
Published November 27th, 2006 in Java.Each stateful session bean is dedicated to one client for the life of the bean instance. Stateful session beans are primarily used to encapsulate and manage task flow on behalf of the client - they manage the interactions of other beans for the client.
Stateful session beans do not use instance pooling. Each stateful session bean must be passivated before it is evicted from memory in order to preserve the conversational state of the instance. It must then be activated when the bean becomes active again. The process achieves certain level of resource pooling but it is different from stateless session bean’s instance pooling.

The lifecycle of a stateful session bean that do not implement javax.ejb.SessionSynchronization interface can be summarized as follow:
- Does Not Exist - when the bean instance throws system exception, timeout in Passive state, timeout in Method-Ready state, client initiated removal by invoking method annotated as
@javax.annotation.Remove(@javax.annotation.PreDestroycallback) - Method-Ready - from Does Not Exist (
Class.newInstance(), injections,@javax.annotation.PostConstruct()callback), from Passive (container dependent create,@javax.annotation.PostActivate()callback) - Passive - from Method-Ready (
@javax.annotation.PrePassivate()callback)
In the transition to Passive state, not only the bean instance is saved but also the following:
javax.ejb.SessionContextjavax.jta.UserTransactionjavax.naming.Contextjavax.persistence.EntityManagerjavax.persistence.EntityManagerFactory- References to other EJBs
- References to managed resource factories
These objects or references will be restored by the EJB container during activation together with the bean instance.
When a stateful session bean is injected into another stateful session bean using @javax.annotation.EJB, a session is created in the injected instance. When the containing bean is removed, the injected bean’s session is ended and it is removed together with the containing bean. The feature allows you to cleanly aggregate conversational business process without worrying the lifecycle of each bean.


0 Responses to “SCBCD Notes: Stateful Session Bean”
Please Wait
Leave a Reply