SET AS HOME PAGE

ADD TO FAVORITES / BOOKMARK THIS WEBSITE (More Details)

Introduction

Servlet

Jsp

Security

Enterprise Beans

Contact Us


Creating Stateful Session Beans

 Introduction

The state of an object consists of the values of its instance variables. In a stateful session bean, the instance variables represent the state of a unique client-bean session. Because the client interacts with its bean, this state is often called the conversational state.

The state is retained for the duration of the client-bean session. If the client removes the bean or terminates, the session ends and the state disappears. This transient nature of the state is not a problem, however, because when the conversation between the client and the bean ends there is no need to retain the state.
Types of Enterprise Beans are
  • Session Beans
    1. Stateless Session Beans
    2.  Stateful Session Beans
  • Entity Beans
    1. Bean Managed Persistent (BMP) Entity Beans
    2. Container Managed Persistent (CMP) Entity Beans
  • Message-Driven Bean

 Overview Of Stateful Session Beans

  • The stages in the life cycle of a stateful session bean are:
    • Ready: In this stage, a stateful session bean instance remains in the shared pool and services client requests.
    • Passive: In this stage, a stateful session bean instance stores client state in secondary storage and stops servicing client requests.
    • Does Not Exist: In this stage, a stateful session bean is permanently removed from the shared pool.

  • The following figure shows the life cycle stages of a stateful session bean:

  • The Ready Stage
    • At the beginning of its life cycle, a stateful session bean is in the Ready stage.
    • A stateful session bean instance moves to the Ready stage in the following cases:
      • When EJB container creates a new stateful session bean instance.
      • When EJB container activates a passivated stateful session bean.
    • EJB container creates new stateful session bean instances when there are insufficient stateful session bean instances in the shared pool to service client requests.
    • To create a new stateful session bean instance, EJB container performs the following steps:
      1. newInstance() method, which instantiates a new stateful session bean instance.
      2. setEntityContext() method to associate the stateful session bean instance with the bean context.
      3. ejbCreate() method of the stateful session bean class. You can pass arguments to the ejbCreate() method to initialize a new stateful session bean instance. A stateful session bean can have more than one ejbCreate() method, where each method has a different number and type of arguments.
    • A stateful session bean instance can also reach the Ready stage, when EJB container activates a passivated stateful session bean by invoking the ejbActivate() method.
    • The ejbActivate() method transfers a passivated stateful session beanís client state to the stateful session bean instance variables.
    • EJB container calls the ejbActivate() method to allow the bean to acquire the resources released by it during passivation.

  • The Passive Stage
    • A stateful session bean moves to the Passive stage in its life cycle when EJB container releases it from serving an idle client and returns the stateful session bean instance to the shared pool to service another clientís requests.
    • Before passivation, EJB container saves the client state associated with the stateful session bean instance in a secondary storage device, such as hard disk in the server.
    • EJB container calls the ejbPassivate() method before it passivates a session bean. This allows a stateful session bean to release its resources.

  • The Does Not Exist Stage
    • At the end of its life cycle, a stateful session bean instance moves to the Does Not Exist stage.
    • A stateful session bean instance reaches this stage in the following cases:
      • When EJB container invokes the ejbRemove() method.
      • When the timeout of the bean instance lifecycle, specified by the bean developer, expires.
    • EJB container destroys stateful session bean instances when the number of instances is greater than the number of active client requests.
    • EJB container fails to invoke the ejbRemove() method in the following cases:
      • When EJB container failure occurs.
      • When a stateful session bean method execution throws a system exception.

  • Activation and Passivation of Stateful Session Beans
    • EJB container passivates and activates the stateful session bean instances in the shared pool to reduce the overhead of storing a large number of bean instances.
    • EJB container stores the bean state of an idle bean instance in a secondary storage device. This process is called passivation.
    • When the client of a passivated bean calls another bean method, the bean state is restored to a stateful session bean instance. This process is called activation.
    • Stateful session beans preserve the references of the following interfaces, in addition to client-specific information during passivation:
      • javax.ejb.EJBHome
      • javax.ejb.EJBObject
      • javax.jta.UserTransaction
      • javax.naming.Context
      • javax.ejb.EJBLocalHome
      • javax.ejb.EJBLocalObject

 Click Next To Continue ...
<- PREVIOUSNEXT ->