J2EEOnline J2EE





Entity Beans   «Prev  Next»
Lesson 8The entity bean lifecycle
Objective Describe the entity bean lifecycle.

What is persistent?

Look at the diagram below and ask yourself what is persistent.

What is persistent
What is persistent

Is it the EJBObject, the actual bean, or the underlying data?
If you think about it, you will realize it is the underlying data.

The bean instance can be discarded

The bean instance, can be discarded by the container because it only represents the underlying persistent data. As long as the container remembers the primary key, it can recreate the bean instance and load the data. Thus, an entity bean instance for existing persistent data can be in one of the following states:
  1. It does not exist, but the underlying data does.
  2. It is pooled. An instance that is pooled is not associated with any particular entity object.
  3. Ready state. An instance in the ready state is assigned to a specific piece of persistent data.

This is illustrated in the following diagram:

The bean instance can be discarded
The bean instance can be discarded

It shows a single ready instance, a pool of instances unassociated with any data, and an EJBObject with no bean instance currently in existence.
Any invocations on the methods in the EJBObject (that does not currently have an instance) causes the container to take an instance from the pool and load its data before the actual method on the bean is invoked.
This is very efficient, as it allows the container to store away instances that are no longer associated with persistent data, and to reuse them when required. It saves having to newInstance() the bean each time, which, as we know, is expensive.

Entity Bean Life Cycle Quiz

Take a moment to test your knowledge on entity bean persistence and lifecycle by clicking the Quiz link below
Entity Bean Life Cycle Quiz
The next lesson reviews the contents of this module.