Lesson 8 | Identity of the bean and using session bean handles |
Objective | Describe how a bean is identified, acquire a bean's handle, then re-connect to that bean later using the handle. |
Bean Identity and Session Bean Handles
In this section we look at bean identities and bean handles[1].
Session bean identity
Session bean instances have no external identity.
The one-to-one relationship between the client and the instance means that no other party needs to know about the identity of the bean instance.
Within a client, you can compare two remote references to the same or different bean instances using the following method of the EJBObject:
if (beanA.isIdentical(beanB)) {
. . .
Do not simply compare remote references using ==
.
It is not guaranteed to give correct results. For example (beanA == beanB)
may not work correctly.
Use isIdentical()
.
Using the handle
A handle relates to a specific EJBObject object. Thus, the handle can be used to "find" an already created bean instance.
The handle is retrieved from the EJBObject as follows:
javax.ejb.Handle handle = bean.getHandle();
// serialize the handle and store
. . .
Once the client has the handle to the EJBObject of a bean instance, it can be serialized and written to stable storage.
Later, possibly in a different JVM, the handle can be de-serialized from stable storage and used to obtain a reference to the EJBObject as follows:
// load and de-serialize the handle
. . .
// get the Remote object reference
bean = handle.getEJBObject();
String response = bean.sayHello();
. . .
The handle will be valid as long as the bean instance has not been removed, or the container has not crashed and discarded the instance.
Some containers will impose a timeout on an instance if it has not been accessed for a specific length of time, at which point the instance will be discarded.
Business Methods - Quiz
Click the Quiz link below to test your understanding of session bean business methods and exceptions and session bean handles.
Business Methods - Quiz
In the next lesson, you will review what you've learned in this module.
[1]Bean handle: A universal reference to a bean instance, that is available from the EJBObject, that can be used to re-connect to a bean at a later time.