From the perspective of the bean provider, the entity bean exists when the create() method of the home interface is called. The create() method of the remote home interface returns a reference to the remote interface. The create() method of the local home interface returns a reference to the local interface. The components of the entity bean are discussed in more detail in the next section. This early introduction to the remote and local home interfaces and the remote and local interfaces aids in the discussion of the lifecycle of an entity bean. All four of these components are interfaces, not classes. The implementation classes for the entity bean interfaces are generated by WebLogic Server when the bean is deployed. The lifecycle of the entity bean object is controlled by the WebLogic EJB container. As shown in Figure 21.1, the container maintains a pool of objects that are reused and dynamically associated with an entity bean. This allows the container to provide high-performance bean management by pre-allocating beans at system startup. Figure 21.1. The EJB container manages the free pool of entity beans that are dynamically assigned when the client requests the creation of an entity bean instance.
Figure 21.1 shows the sequence of events that occur in the lifecycle of an entity bean object:
When the container creates the entity bean instances to populate the free pool, it provides the entity bean with a reference to the EntityContext through the setEntityContext() method of the EntityBean interface. The EntityContext gives the entity bean access to the container provided at runtime. Through methods of the EntityContext , the entity bean can invoke getEJBLocalObject() , getEJBObject() , and getPrimaryKey() . The entity bean object is said to be in the pooled state because it is not yet associated with the persistence of any particular entity bean. When the application calls the create() method on the home interface of an entity bean, the container selects an instance of a pooled entity bean and calls the corresponding ejbCreate() and ejbPostCreate() methods on the entity bean. The entity bean is now in the ready state and is used to manage persistence. The entity bean can transition back to the pooled state under three conditions:
An entity bean that is in the pooled state as a result of passivation can transition back to the ready state through activation. To activate an entity bean that is not associated with a bean instance, the container calls the ejbActivate() method followed by the ejbLoad() method. The container can remove an entity bean from the bean pool by calling the unsetEntityContext() on the bean. This only occurs on a pooled bean, not a ready bean. |