The Life Cycles of Enterprise Beans
2009-01-01 19:54
330 查看
The Life Cycles of Enterprise Beans
An enterprise bean goes through various stages during its lifetime, orlife cycle. Each type of enterprise bean--session, entity, or
message-driven--has a different life cycle.
The descriptions that follow refer to methods that are explained along
with the code examples in the next two chapters. If you are new to
enterprise beans, you should skip this section and try out the code
examples first.
The Life Cycle of a Stateful Session Bean
Figure 23-4illustrates the stages that a session bean passes through during its
lifetime. The client initiates the life cycle by invoking the
createmethod. The EJB container instantiates the bean and then invokes the
setSessionContextand
ejbCreatemethods in the session bean. The bean is now ready to have its business methods invoked.
Figure 23-4 Life Cycle of a Stateful Session Bean
While in the ready stage, the EJB container may decide to deactivate, or passivate,
the bean by moving it from memory to secondary storage. (Typically, the
EJB container uses a least-recently-used algorithm to select a bean for
passivation.) The EJB container invokes the bean's
ejbPassivate
method immediately before passivating it. If a client invokes a
business method on the bean while it is in the passive stage, the EJB
container activates the bean, calls the bean's
ejbActivatemethod, and then moves it to the ready stage.
At the end of the life cycle, the client invokes the
removemethod, and the EJB container calls the bean's
ejbRemovemethod. The bean's instance is ready for garbage collection.
Your code controls the invocation of only two life-cycle methods: the
createand
removemethods in the client. All other methods in Figure 23-4 are invoked by the EJB container. The
ejbCreate
method, for example, is inside the bean class, allowing you to perform
certain operations right after the bean is instantiated. For example,
you might wish to connect to a database in the
ejbCreatemethod. See Chapter 31 for more information.
The Life Cycle of a Stateless Session Bean
Because a stateless session bean is never passivated, its life cyclehas only two stages: nonexistent and ready for the invocation of
business methods. Figure 23-5 illustrates the stages of a stateless session bean.
Figure 23-5 Life Cycle of a Stateless Session Bean
The Life Cycle of an Entity Bean
Figure 23-6shows the stages that an entity bean passes through during its
lifetime. After the EJB container creates the instance, it calls the
setEntityContextmethod of the entity bean class. The
setEntityContextmethod passes the entity context to the bean.
After instantiation, the entity bean moves to a pool of available
instances. While in the pooled stage, the instance is not associated
with any particular EJB object identity. All instances in the pool are
identical. The EJB container assigns an identity to an instance when
moving it to the ready stage.
There are two paths from the pooled stage to the ready stage. On the first path, the client invokes the
createmethod, causing the EJB container to call the
ejbCreateand
ejbPostCreatemethods. On the second path, the EJB container invokes the
ejbActivatemethod. While an entity bean is in the ready stage, an it's business methods can be invoked.
There are also two paths from the ready stage to the pooled stage. First, a client can invoke the
removemethod, which causes the EJB container to call the
ejbRemovemethod. Second, the EJB container can invoke the
ejbPassivatemethod.
Figure 23-6 Life Cycle of an Entity Bean
At the end of the life cycle, the EJB container removes the instance from the pool and invokes the
unsetEntityContextmethod.
In the pooled state, an
instance is not associated with any particular EJB object identity.
With bean-managed persistence, when the EJB container moves an instance
from the pooled state to the ready state, it does not automatically set
the primary key. Therefore, the
ejbCreateand
ejbActivatemethods must assign a value to the primary key. If the primary key is incorrect, the
ejbLoadand
ejbStoremethods cannot synchronize the instance variables with the database. In the section The SavingsAccountBean Example, the
ejbCreatemethod assigns the primary key from one of the input parameters. The
ejbActivatemethod sets the primary key (
id) as follows:
id = (String)context.getPrimaryKey();
In the pooled state, the values of the instance variables are not
needed. You can make these instance variables eligible for garbage
collection by setting them to
nullin the
ejbPassivatemethod.
The Life Cycle of a Message-Driven Bean
Figure 23-7 illustrates the stages in the life cycle of a message-driven bean.The EJB container usually creates a pool of message-driven bean
instances. For each instance, the EJB container instantiates the bean
and performs these tasks:
It calls the
setMessageDrivenContextmethod to pass the context object to the instance.
It calls the instance's
ejbCreatemethod.
Figure 23-7 Life Cycle of a Message-Driven Bean
Like a stateless session bean,
a message-driven bean is never passivated, and it has only two states:
nonexistent and ready to receive messages.
At the end of the life cycle, the container calls the
ejbRemovemethod. The bean's instance is then ready for garbage collection.
From: http://java.sun.com/j2ee/1.4/docs/tutorial/doc/EJBConcepts9.html
相关文章推荐
- The game of life(生命游戏)新算法
- Spring启动异常: cvc-elt.1: Cannot find the declaration of element 'beans'
- spring cvc-elt.1: Cannot find the declaration of element 'beans'解决办法
- The First Day of My Life
- About the Importance of Aim in Life
- cvc-elt.1: Cannot find the declaration of element 'beans'
- Most of the shadows of life are caused by standing in our own sunshine.
- Enterprise Services Bus ---3&4 Necessity Is the Mother of Invention
- THE STORIED LIFE OF A.J.FIKRY
- BZOJ 3546 Life of the Party (二分图匹配-最大流)
- Eloquent JavaScript 笔记 六:The Secret Life of Objects
- the arrival of a small life brought
- Cannot find the declaration of element 'beans'. 的解决方法
- The first step of my life in the EC field
- spring cvc-elt.1: Cannot find the declaration of element 'beans'解决办法
- [新概念三] Lesson 4 - The double life of Alfred Bloggs
- Spring 2.0.8 和 2.5 applicationContext.xml 头文件写法 Spring启动异常: cvc-elt.1: Cannot find the declaration of element 'beans'
- Spring初始化时异常(Cannot find the declaration of element 'beans' )
- CSDN——The Beginning Of My Tech Life
- A Day in the Life of IT