What is a stateful session bean?

Richard Monson-Haefel

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 beans are usually developed to act as agents for the client, managing the interaction of other beans and performing work on behalf of the client application. An example is a shopping cart stateful session bean that tracks a client's product choices and can execute a sale when requested. Below is partial example of a stateful session bean that is used as a shopping cart.

public class ShoppingCartBean implements javax.ejb.SessionBean {
          // instance fields; The conversational state
          Vector products = new Vector( );
          Customer customer;
          // initializes bean with reference to customer bean
          public void ejbCreate(Customer cust){
                    customer cust;

           //add a product to shopping cart
          public void addItem(Product item) {

          //charge customer for products and create a shipping order
          public void executeSale(){

                         // get the customer's credit card object
                        CreditCard card = customer.getCreditCard( );

                        // calculate a total price from products chosen
                        double total;
                        for(int i = 0; i  products.size(); i++){
                               Product product = (Product)products.elementAt(i);
                               total += product.getPrice( );

                        // get reference to the CreditService bean and process the customers charge
                        CreditService cardSwipper = ... get credit card service bean
                        Charge charge = cardSwipper.charge(CreditCard, total);

                        // get reference to the OrderHome and create a new shipping order bean (record)
                        OrderHome orderHome = ... get the Order beans Home object
                        Order order = orderHome.create(customer, products, charge);

In the example above the ShoppingCartBean keeps track of the Customer and the items chosen by the client application. This is the bean's conversational state. Once the client application is ready to execute a sale, the ShoppingCartBean manages the interactions of the Customer, Product, CreditServer, and Order beans in a workflow that results in a charge against the customers credit card and the creation of a shipping order. The ShoppingCartBean behaves as an agent for the client managing the interaction of these other beans, tracking chosen items, and executing the sale. The below table identifies the beans types used in the ShoppingCartBean example above.

Bean NameBean Type
ShoppingCartBeanStateful Session
CreditServiceStateless Session

From the clients perspective only the Customer and ShoppingCart beans are visible, because the client application works with the bean's remote interface while the bean itself resides on the server. This means that the client need only be concerned with the business methods made public by the ShoppingCartBean. Below is an example of the client applications view of the ShoppingCartBean. (Note: The client interacts with the beans remote interface not the bean itself. The remote interface is called the ShoppingCart.)

// somewhere in the client application (applet, servlet, etc.)

// obtain the home objects (factories) for the Customer and ShoppingCart beans
CustomerHome custHm = ... get CustomerHome
ShoppingCartHome shpCrtHm = ... get ShoppingCartHome 

// locate the bean representing the customer named, Bob Johnsten
Customer customer = custHome.findByName("Bob","Johnsten");

// create a new ShoppingCart initialized with the Bob Johnsten Customer bean
ShoppingCart shoppingCart = shpCrtHm.create(customer);
Product product = ... get reference to bean representing the product chosen
// add product to customer's shopping cart
//charge customer for products and create a shipping order
shoppingCart.executeSale( );

Session beans like the shoppingCartBean only live as long as the client maintains a connection. In other words, they represent some aspect of the client's current session with the system and die when the client ends that session. Session beans are generally not fault tolerant. A system failure or shut down will result in the death of session bean and a loss of any conversation state it maintained prior to the failure. (This is a conceptual lifecycle of a session bean. Some vendors can make them more fault tolerant and longer lasting).