type=page
status=published
title=The Lifecycles of Enterprise Beans
next=ejb-intro008.html
prev=ejb-intro006.html
~~~~~~
The Lifecycles of Enterprise Beans
==================================

[[GIPLJ]]

[[the-lifecycles-of-enterprise-beans]]
The Lifecycles of Enterprise Beans
----------------------------------

An enterprise bean goes through various stages during its lifetime, or
lifecycle. Each type of enterprise bean (stateful session, stateless
session, singleton session, or message-driven) has a different
lifecycle.

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 run the code examples
first.

The following topics are addressed here:

* link:#GIPLN[The Lifecycle of a Stateful Session Bean]
* link:#GIPLM[The Lifecycle of a Stateless Session Bean]
* link:#GIPRX[The Lifecycle of a Singleton Session Bean]
* link:#GIPKW[The Lifecycle of a Message-Driven Bean]

[[GIPLN]]

[[the-lifecycle-of-a-stateful-session-bean]]
The Lifecycle of a Stateful Session Bean
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

link:#GIPMI[Figure 35-2] illustrates the stages that a stateful session
bean passes through during its lifetime. The client initiates the
lifecycle by obtaining a reference to a stateful session bean. The
container performs any dependency injection and then invokes the method
annotated with `@PostConstruct`, if any. The bean is now ready to have
its business methods invoked by the client.

[[GIPMI]]

.*Figure 35-2 Lifecycle of a Stateful Session Bean*
image:img/javaeett_dt_021.png[
"Diagram showing the lifecycle 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 method
annotated `@PrePassivate`, if any, 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 method
annotated `@PostActivate`, if any, and then moves it to the ready stage.

At the end of the lifecycle, the client invokes a method annotated
`@Remove`, and the EJB container calls the method annotated
`@PreDestroy`, if any. The bean's instance is then ready for garbage
collection.

Your code controls the invocation of only one lifecycle method: the
method annotated `@Remove`. All other methods in link:#GIPMI[Figure
35-2] are invoked by the EJB container. See
link:resources.html#BNCJH[Chapter 55, "Resource Adapters and Contracts"]
for more information.

[[GIPLM]]

[[the-lifecycle-of-a-stateless-session-bean]]
The Lifecycle of a Stateless Session Bean
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Because a stateless session bean is never passivated, its lifecycle has
only two stages: nonexistent and ready for the invocation of business
methods. link:#GIPNI[Figure 35-3] illustrates the stages of a stateless
session bean.

[[GIPNI]]

.*Figure 35-3 Lifecycle of a Stateless or Singleton Session Bean*
image:img/javaeett_dt_022.png[
"Diagram showing the lifecycle of a stateless or singleton session bean."]

The EJB container typically creates and maintains a pool of stateless
session beans, beginning the stateless session bean's lifecycle. The
container performs any dependency injection and then invokes the method
annotated `@PostConstruct`, if it exists. The bean is now ready to have
its business methods invoked by a client.

At the end of the lifecycle, the EJB container calls the method
annotated `@PreDestroy`, if it exists. The bean's instance is then ready
for garbage collection.

[[GIPRX]]

[[the-lifecycle-of-a-singleton-session-bean]]
The Lifecycle of a Singleton Session Bean
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Like a stateless session bean, a singleton session bean is never
passivated and has only two stages, nonexistent and ready for the
invocation of business methods, as shown in link:#GIPNI[Figure 35-3].

The EJB container initiates the singleton session bean lifecycle by
creating the singleton instance. This occurs upon application deployment
if the singleton is annotated with the `@Startup` annotation. The
container performs any dependency injection and then invokes the method
annotated `@PostConstruct`, if it exists. The singleton session bean is
now ready to have its business methods invoked by the client.

At the end of the lifecycle, the EJB container calls the method
annotated `@PreDestroy`, if it exists. The singleton session bean is now
ready for garbage collection.

[[GIPKW]]

[[the-lifecycle-of-a-message-driven-bean]]
The Lifecycle of a Message-Driven Bean
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

link:#GIPLR[Figure 35-4] illustrates the stages in the lifecycle of a
message-driven bean.

[[GIPLR]]

.*Figure 35-4 Lifecycle of a Message-Driven Bean*
image:img/javaeett_dt_023.png[
"Diagram showing the lifecycle of a message-driven bean."]

The EJB container usually creates a pool of message-driven bean
instances. For each instance, the EJB container performs these tasks.

1.  If the message-driven bean uses dependency injection, the container
injects these references before instantiating the instance.
2.  The container calls the method annotated `@PostConstruct`, if any.

Like a stateless session bean, a message-driven bean is never passivated
and has only two states: nonexistent and ready to receive messages.

At the end of the lifecycle, the container calls the method annotated
`@PreDestroy`, if any. The bean's instance is then ready for garbage
collection.
