Life Cycle of an Entity EJB

Figure 12.1 shows the three stages in the life cycle of an entity bean: creation, process operations, and termination.

Figure 12.1. Life cycle of an entity bean.

graphics/12fig01.gif

Creation

An entity bean's life cycle varies from the life cycle of a session bean in some respects. This is because entity beans represent persistent data while session beans contain transient data. That's why an entity bean can come into existence in two different ways:

  • the result of a client application that calls the create() method in the home interface of the entity bean. In this case, the EJB container invokes the ejbCreate() method of the entity bean implementation class. The entity bean implementation class provides the functionality of the ejbCreate() method in BMP or the container's tools generated class in CMP.

  • when a client application invokes the findByPrimaryKey() or any findXXX() finder methods. The result of these queries will be an entity bean initialized with the values retrieved from the database.

An entity bean need not be instantiated by the EJB container. If an entity bean is available in the entity bean pool, the EJB container retrieves the bean from the bean pool. After the instantiation of the entity bean, the EJB container invokes the first callback method of the entity bean which is the setEntityContext() method and passes the EntityContext object.

The entity bean is now active and ready to process the client application requests.

Process Operations

Entity beans represent the data in the database. Client applications that need to retrieve data from the database use the finder methods of the entity bean's home interface. For persisting data, a client application calls either the create() method of the entity bean's home interface or a client application updates the entity bean's state.

Like a session bean, an entity bean is passivated or activated at different times in its life cycle. During activation or passivation, the EJB container invokes the ejbActivate() and ejbPassivate() life-cycle callback methods. When an entity bean is not in use for some time, the EJB container passivates the entity bean. In other words, it stores its state.

The difference here, with respect to a session bean, is that a session bean's state is simply serialized to a disk store while an entity bean's state is stored in the database. This is called synchronizing the database with the state of the entity bean. To perform this synchronization of state between the entity bean and the database, the EJB container invokes the ejbStore() callback method. After invoking the ejbStore() method, the entity bean is returned to a pool of entity beans dissociated with any data identifiers.

On the flip side, when activating an entity bean, the EJB container needs to restore the entity bean to the state of the database contents. To do this, the EJB container invokes the ejbLoad() method. The ejbLoad() method contains the necessary code to synchronize the state of the entity bean with the database contents.

Termination

There are two ways for a "stateful" session bean to get terminated by the EJB container. The first, and most common way to terminate a stateful session bean is for the client application to call the remove() method. Unlike a session bean, when an entity bean receives the remove() request from the client application, the rows of data in the database represented by the entity bean is (are) deleted. Then the EJB container unloads the loaded entity bean from memory and invokes the ejbRemove() callback method. The ejbRemove() method enables the session bean to perform cleanup and to release resources. The entity bean is then disassociated from the data it represents by the EJB container.

The second way for an EJB to be terminated is for the session bean to be timed out. If a timeout occurs, the EJB container removes the session bean from memory and dissociates it from the client application.



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

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