Life Cycle of an Entity Bean

An entity bean instance’s life starts when the container creates the instance using newInstance().

The container then invokes the setEntityContext() method to pass the instance a reference to the EntityContext interface. The EntityContext interface allows the instance to invoke services provided by the container and to obtain the information about the caller of a client-invoked method.

The ejbFindByPrimaryKey() method is executed on the beans which are in the pool. With the execution of this call back method the instance does NOT move from the pool state to the ready state.

The container invokes the ejbFind(...) method on an instance when a client invokes a matching find(...) method on the entity bean’s home interface. The container must pick an instance that is in the pooled state for the execution of the ejbFind(...) method.
If there is no instance in the pooled state, the container creates one and calls the setEntity-Context(...) method on the instance before dispatching the finder method.

If the ejbFind method is declared to return a single primary key, the container creates an entity EJBObject reference for the primary key and returns it to the client. If the ejbFind method is declared to return a collection of primary keys, the container creates a collection of entity EJBObject references for the primary keys returned from the ejbFind, and returns the collection to the client.

The instances in the pool are not associtaed with any entity object identity. All instances in the pool are considered equivalent.

The bean travels from the pool state to the ready state when the Cr selects that instance to service a Client call to an entity object.

The bean can traverse from the pooled state to the ready state in 2 ways:
1) ejbCreate(), ejbPostCreate()
2) ejbActivate()

When Client calls create() on the home the Cr invokes ejbCreate(), ejbPostCreate() on the bean in the pool state.

The container invokes the ejbActivate() method on an instance when an instance needs to be activated to service an invocation on an existing entity object this occurs because there is no suitable instance in the ready state to service the client’s call.

When an entity bean instance is in the ready state, the instance is associated with a specific entity object identity. While the instance is in the ready state, the container can synchronize the state of the instance with the state of the entity in the underlying data source whenever it determines
the need to, in the process invoking the ejbLoad() and ejbStore() methods zero or more times. A business method can be invoked on the instance zero or more times.

Before calling the ejbPassivate() the Cr calls ejbStore() on the bean in the ready state. This allows the instance to prepare itself for the synchronization of the database state with the instance’s state, and to allow the persistence manager to store the instance’s state to the database. Now the instance moves from the ready state to the pooled state.

There are 3 ways for the instance to travel from the ready state to the pooled state:
1) ejbPassivate()
2) ejbRemove()
3) because of a transaction rollback for ejbCreate(), ejbPostCreate(), or

ejbPassivate() is invoked by the container when it wants to disassociate the instance from the entity object identity without removing the entity object.

ejbRemove() is called when the client calls remove() on the home interface or the remote interface. When ejbRemove() is called the Cr removes the entity object. The instance can still obtain the identity of the entity object via the getPrimaryKey() or getEJBObject() method of the EntityContext interface.

ejbCreate, ejbPostCreate() or ejbRemove() is called and the transaction rolls back, the container will transition the bean instance to the pooled state.

When the instance is put back into the pool, it is no longer associated with an entity object identity. The container can assign the instance to any entity object within the same entity bean home.

The container can remove an instance in the pool by calling the unsetEntityContext() method on the instance.

A RuntimeException thrown from any method of the entity bean class (including the business methods and the callbacks invoked by the container) results in the transition to the does not exist state. The container must not invoke any method on the instance after a RuntimeException has been caught. From the client perspective, the corresponding entity object continues to exist. The client can continue accessing the entity object through its remote interface because the container can use a different entity bean instance to delegate the client’s requests.

What resources the instance has claimed thru ejbActivate(), those resources should be released by ejbPassivate().

If the Bean Provider has assigned dependent objects to instance variables (i.e., to non-container-managed fields of the bean instance), the Bean Provider should discard the references by setting the instance variables to

Entity Bean object - object of the implementation class of EJBObject (Remote).

Entity Bean Instance - object of the bean class which we write.

Entity object identity -

remote reference -

No comments: