Home TOC |
Search
Feedback |
The Life Cycle of a Session Bean
A session bean goes through various stages during its lifetime, or life cycle. The life cycle is managed by the EJB container, not by your applications. Although your applications cannot explicitly manage a bean's life cycle, you'll find the information in the following sections useful when you need to manage resources such as database connections. See Resource Connections for details.
The Stateful Session Bean Life Cycle
Figure 6 illustrates the stages that a session bean passes through during its lifetime. The client initiates the life cycle by invoking the create method.The EJB container instantiates the bean and then invokes the setSessionContext and ejbCreate methods in the session bean. The bean is now ready to have its business methods invoked.
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, moving it back to the ready stage, and then calls the bean's ejbActivate method.
At the end of the life cycle, the client invokes the remove method and the EJB container calls the bean's ejbRemove method. The bean's instance is ready for garbage collection.
Your code controls the invocation of only two life cycle methods-the create and remove methods in the client. All other methods in Figure 6 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 instance, you may wish to connect to a database in the ejbCreate method. See Resource Connections for more information.
Figure 6 Life Cycle of a Stateful Session Bean
The Stateless Session Bean Life Cycle
Because a stateless session bean is never passivated, its life cycle has just two stages: non-existent and ready for the invocation of business methods. Figure 7 illustrates the stages of a stateless session bean.
Home TOC |
Search
Feedback |