Explain the passivation and activation in Stateful Session bean?
Explain the passivation and activation in Stateful Session bean?
During the lifetime of a stateful session bean, there may be periods of inactivity, when the bean instance is not servicing methods from the client. To conserve resources, the container can passivate the bean instance while it is inactive by preserving its conversational state and evicting the bean instance from memory. When the client becomes active, the container may activate the instance by populating it with its preserved conversational state. This is referred to as passivation and activation in the life cycle of stateful session bean.
During passivation, the instance fields are read and then written to the secondary storage associated with the EJB object. Only the non-transient serializable instance fields are preserved. When a bean is about to be passivated, the method annotated with @PrePassivate is invoked, alerting the bean instance that it is about to enter the passive state. At this time, the bean instance should close any open resources and set all nontransient, non-serializable fields to null. When the stateful session bean has been successfully passivated, the instance is evicted from memory; it is destroyed.
When the client makes a request on an EJB object whose bean is passivated, the container activates the instance. When the bean is activated the new instance is populated with the preserved state. When a bean’s conversational state has been successfully activated, the method annotated with @PostActivate is invoked. The bean instance should open any resources needed and initialize the value of any transient fields within the method with @PostActivate annotation. Once this is complete, the bean is back in the Method-Ready state and available to service client requests delegated by the EJB object.