| Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g Release 3 (10.1.3) B14428-01 |
|
![]() Previous |
![]() Next |
In a BMP entity bean, you are responsible for implementing all of the EJB 2.1 BMP entity bean lifecycle callback methods:
The ejbStore method is called by the container before the object is passivated or whenever a transaction is about to end. Its purpose is to save the persistent data to an outside resource, such as a database
The container invokes the ejbStore method when the persistent data should be saved to the database. This synchronizes the state of the instance to the entity in the underlying database. For example, the container invokes before the container passivates the bean instance or removes the instance. The BMP bean is responsible for ensuring that all data is stored to some resource, such as a database, within this method.
public void ejbStore() throws EJBException
{
//Container invokes this method to instruct the instance to
//synchronize its state by storing it to the underlying database
//System.out.println("EmployeeBean.ejbStore(): begin");
try {
pk = (EmployeePK) ctx.getPrimaryKey();
conn = getConnection(dsName);
ps = conn.prepareStatement(updateStatement);
ps.setString(1, pk.empName);
ps.setFloat(2, pk.salary.floatValue());
ps.setInt(3, pk.empNo.intValue());
if (ps.executeUpdate() != 1) {
throw new EJBException("Failed to update record");
}
} catch (SQLException e) {
throw new EJBException(e.getMessage());
} catch (NamingException e) {
System.out.println("Caught an exception 1 " + e.getMessage() );
throw new EJBException(e.getMessage());
} finally {
try {
ps.close();
conn.close();
} catch (SQLException e) {
throw new EJBException(e.getMessage());
}
}
}
The ejbLoad method is called by the container before the object is activated or whenever a transaction has begun, or when an entity bean is instantiated. Its purpose is to restore any persistent data that exists for this particular bean instance
The container invokes the ejbLoad method whenever it needs to synchronize the state of the bean with what exists in the database. This method is invoked after activating the bean instance to refresh it with the state that is in the database. The purpose of this method is to repopulate the persistent data with the saved state. For most ejbLoad methods, this implies reading the data from a database into the instance data variables.
public void ejbLoad() throws EJBException
{
//Container invokes this method to instruct the instance to
//synchronize its state by loading it from the underlying database
//System.out.println("EmployeeBean.ejbLoad(): begin");
try {
pk = (EmployeePK) ctx.getPrimaryKey();
ejbFindByPrimaryKey(pk);
} catch (FinderException e) {
throw new EJBException (e.getMessage());
}
}
The ejbPassivate method is invoked directly before the bean instance is serialized for future use. It will be re-activated, through the ejbActivate method, the next time the user invokes a method on this instance.
Before the bean is passivated, you should release all resources and release any static information that would be too large to be serialized. Any large, static information that can be easily regenerated within the ejbActivate method should be released in this method.
In our example, the only resource that cannot be serialized is the open database connection. It is closed in this method and reopened in the ejbActivate method.
public void ejbPassivate()
{
// Container invokes this method on an instance before the instance
// becomes disassociated with a specific EJB object
conn.close();
}
The container invokes this method when the bean instance is reactivated. That is, the user has asked to invoke a method on this instance. This method is used to open resources and rebuild static information that was released in the ejbPassivate method.
In addition, the container invokes this method after the start of any transaction.
Our employee example opens the database connection where the employee information is stored.
public void ejbActivate()
{
// Container invokes this method when the instance is taken out
// of the pool of available instances to become associated with
// a specific EJB object
conn = getConnection(dsName);
}
The container invokes the ejbRemove method before removing the bean instance itself or by placing the instance back into the bean pool. This means that the information that was represented by this entity bean should be removed from within persistent storage. The employee example removes the employee and all associated information from the database before the instance is destroyed. Close the database connection.
public void ejbRemove() throws RemoveException
{
//Container invokes this method befor it removes the EJB object
//that is currently associated with the instance
//System.out.println("EmployeeBean.ejbRemove(): begin");
try {
pk = (EmployeePK) ctx.getPrimaryKey();
conn = getConnection(dsName);
ps = conn.prepareStatement(deleteStatement);
ps.setInt(1, pk.empNo.intValue());
if (ps.executeUpdate() != 1) {
throw new RemoveException("Failed to delete record");
}
} catch (SQLException e) {
throw new RemoveException(e.getMessage());
} catch (NamingException e) {
System.out.println("Caught an exception 1 " + e.getMessage() );
throw new EJBException(e.getMessage());
} finally {
try {
ps.close();
conn.close();
} catch (SQLException e) {
throw new EJBException(e.getMessage());
}
}
}