Lesson 7 | Entity bean container callbacks: |
Objective | Learn the code to handle the EntityContext. |
Entity Bean Container Callbacks:
- setEntityContext() and
- unsetEntitycontext()
Learn code to handle the EntityContext.
In this lesson you will learn the code to handle the instance's context.
The setEntityContext()
method is called by the container and provides the bean with a reference to the instance's
context.
The instance's context is stored in an EntityContext object in the bean.
This method is similar in philosophy to the setSessionContext()
method for session beans.
The javax.ejb.EntityContext
object contains the following methods:
public java.lang.Object getPrimaryKey() throws
java.lang.IllegalStateException
public EJBObject getEJBObject() throws
java.lang.IllegalStateException
These methods give the bean access to both the primary key and the EJBObject. Because this information is available as soon as the EntityContext is set, resources like connecting to the database,
often are allocated from within the setEntityContext()
method. This is illustrated in the following Customer bean:
01 public void setEntityContext(EntityContext
context) {
02 this.context = context; // save the instance's
context
03 try {
04 makeConnection(); // connect to the database
05 } catch (Exception e) {
06 throw new EJBException(
07 "Unable to connect to database."
08 + e.getMessage());
09 }
10 }
The setEntityContext()
method is called by the container as soon as the instance is associated with its primary key and the EJBObject is created. This process was described in the entity bean architecture module.
It passes in the context, which is stored in this.context
on line 02. On line 04, the bean attempts to make a connection with the database using the method
makeConnection()
. Note that, as shown in previous lessons, any exceptions that are thrown in the makeConnection()
method are caught and an EJBException is thrown back to the client in its place.
Methods of the entity context
The methods of the entity context are as follows:
public interface EJBContext {
Identity getCallerIdentity()
EJBHome getEJBHome();
boolean getRollbackOnly();
UserTransaction getUserTransaction() throws
IllegalStateException
boolean isCallerInRole(Identity role);
void setRollbackOnly();
}
getCallerIdentity()
and isCallerInRole()
will be covered in the module on security. getEJBHome()
is
straightforward. getRollbackOnly()
, getUserTransaction(), and setRollbackOnly()
were described in the module on
transactions.
unsetEntityContext()
As resources are allocated in the setEntityContext()
method there has to be a way to release these resources at the end of the
life of the bean. This is done using the unsetEntityContext() method, which is invoked just before the bean is discarded. From the Customer
bean:
public void unsetEntityContext() {
try {
con.close(); // close the database connection
} catch (SQLException e) {
throw new EJBException("unsetEntityContext: " +
e.getMessage());
}
}
The next lesson introduces the ejbStore()
and ejbLoad()
container callbacks.