What is Stateful session bean? Explain it's life cycle?

Stateful session bean maintain the state of the conversation between the client and itself. When the client invokes a method on the bean the instance variables of the bean may contain a state but only for the duration of the invocation.
 
A stateful session bean is an enterprise bean (EJB component) that acts as a server-side extension of the client that uses it. The stateful session bean is created by a client and will work for only that client until the client connection is dropped or the bean is explicitly removed. 
 
The stateful session bean is EJB component that implements the javax.ejb.SessionBean interface and is deployed with the declarative attribute “stateful”. 
 
Stateful session beans are called “stateful” because they maintain a conversational state with the client. In other words, they have state or instance fields that can be initialized and changed by the client with each method invocation. The bean can use the conversational state as it process business methods invoked by the client.
 
Stateful session bean Life Cycle:
 

Stateful session beans
have a more complex lifecycle and the Container performs the following:
  • Always creates new bean instances using the default constructor whenever a new client session is started
  • Injects resources
  • Stores the instance in memory
  • Executes the requested business method invoked through the business interface by the client
  • Waits for and executes subsequent client requests
  • If idle for a period of time, container passivates the instance, normally moved out of memory onto disk (serialize the entire bean instance)
  • If the client invokes a passivated bean, it is actived (brought back from disk to memory)
  • If a method with the @Remove is invoked the bean is destroyed after the method completes

 


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email