The Lifecycle of Entity Beans


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.

graphics/21fig01.gif

Figure 21.1 shows the sequence of events that occur in the lifecycle of an entity bean object:

  1. The container populates the free pool with a set of Entity Beans.

  2. The client invokes the create() method on the home object.

  3. The home object allocates a bean from the free pool.

  4. The home object invokes the ejbCreate() method on the entity bean, passing the same arguments from create() in step 2.

  5. The entity bean inserts a row into the database table.

  6. The bean returns the primary key of the entity bean object to the home object.

  7. The container creates the EJB object for the entity bean. The primary key is immediately passed to the EJB object.

  8. The home object invokes the ejbPostCreate() method on the entity bean to allow the entity bean to finish initialization with the existence of the EJB object.

  9. The home object returns the remote reference of the EJB object to the client.

  10. The client uses the remote reference to invoke business logic in the bean instance.

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:

  • The container calls ejbPassivate() to disassociate the entity bean from the persistent storage.

  • The client calls the remove() method on the home interface which causes the container to call the ejbRemove() method on the entity bean.

  • The entity bean calls ejbRemove() as a result of rolling back a transaction.

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.



BEA WebLogic Platform 7
BEA WebLogic Platform 7
ISBN: 0789727129
EAN: 2147483647
Year: 2003
Pages: 360

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net