The Entity Bean Life Cycle
The milestones in an entity bean's life cycle are Does Not Exist, Pooled, and Ready. In the Pooled state, the instance is not associated with any particular entity. In the Ready state, the instance has been drawn from the pool, associated with a specific entity, and assigned a unique object identity.
Every entity bean implements the
javax.ejb.EntityBean interface. It should be evident from Figure EB-1 that there are several more life cycle methods here than there are in the
SessionBean interface, and it is usually the case that much more attention must be paid to them, particularly for BMP.
To start an entity bean's life cycle, the container invokes
newInstance(), then sends an
EntityContext to the bean using the
setEntityContext() callback method. At that point, the bean instance is moved into a bean pool. While in the pool, an instance may be used to execute the bean's
ejbFindByPrimaryKey(), other finder methods, or
ejbHomeXXX() methods. The instance does not move to the Ready state on finder method execution; for
ejbHome methods, the bean is returned to the pool after method execution.
An entity instance moves to the Ready state either when the
ejbPostCreate() method pair is called, or when the
ejbActivate() method is called. While in the Ready state, an instance has a specific object identity.
During a bean's life cycle, the container may call
ejbStore() at any time and in any order it deems necessary to read from and write to the persistent datastore.
The container may also call
ejbPassivate() at any time to move the instance back into the pool. Generally, the
ejbStore() method will be invoked just prior to this call. As with any passivation method, resources should be released and reacquired upon activation.
An instance will be moved back to the pool if a transaction rollback occurs during
ejbRemove() invocations will result in transition to the pool as well.
An instance is removed from the pool and goes to the Does Not Exist state immediately after the container calls
unsetEntityContext(), so this is the time to perform any final cleanup operations.