|Prev | Next||J2EETM Developer's Guide
Figure 4-1 shows the stages that an entity bean passes through during its lifetime. After the EJB container creates the instance, it calls the
setEntityContext method of the entity bean class. The
setEntityContext method passes the entity context to the bean.
After instantiation, the entity bean moves to a pool of available instances. While in the pooled stage, the instance is not associated with any particular EJB object identity. All instances in the pool are identical. The EJB container assigns an identity to an instance when moving it to the ready stage.
There are two paths from the pooled stage to the ready stage. On the first path, the client invokes the
create method, causing the EJB container to call the
ejbPostCreate methods. On the second path, the EJB container invokes the
ejbActivate method. While in the ready stage, an entity bean's business methods may be invoked.
There are also two paths from the ready stage to the pooled stage. First, a client may invoke the
remove method, which causes the EJB container to call the
ejbRemove method. Second, the EJB container may invoke the
At the end of the life cycle, the EJB container removes the instance from the pool and invokes the
FIGURE 4-1 Life Cycle of an Entity Bean
In the pooled state, an instance is not associated with any particular EJB object identity. With bean-managed persistence, when the EJB container moves an instance from the pooled state to the ready state, it does not automatically set the primary key. Therefore, the
ejbActivate methods must set the primary key. If the primary key is incorrect, the
ejbStore methods cannot synchronize the instance variables with the database. In the AccountEJB
ejbCreate method assigns the primary key from one of the input parameters. The
ejbActivate method sets the primary key (
id) as follows:
In the pooled state, the values of the instance variables are not needed. You can make these instance variables eligible for garbage collection by setting them to null in theid = (String)context.getPrimaryKey();
TOC | Index