04 - Introduction to EJB Session Beans

 

Simply, Session beans can be called as the application business logic layer in any JEE application. It encapsulates the application business logic and provides container services like Transaction Management and Security to the application developer. As an example, it is the module responsible for calculating how much each user should take as a commission in a commissioning system, it is the layer that is responsible for creating new users into the system database.

EJB 3 provides 3 types of Session Beans, Stateless, Stateful and Singleton, the next few lines describe the meaning of each type:

  • Stateless, a stateless session bean is the one that maintains no conversational state with the client, whether the client is remote client or local client. It is the place where you can’t store client related data between different methods invocations. Also in this type of beans you don’t guarantee that all your invocations go to the same object, as all stateless bean objects are the same from the container point of view. So each time you request an object to invoke, it is the container that decides which object your client will work on.
  • Stateful, Opposite to Stateless beans, Stateful beans maintain a conversational state with the client application. All requests that initiated by the client on a cached object will go to the same server side object, this is the place where you can maintain client related data between different methods invocations.
  • Singleton, this can be considered a Stateless Session bean, but with a fix beans pool of size 1. It has the same features as the Stateless Session bean, but always there is only one object created during the application life cycle. This can be handy for doing tasks related to application initialization (i.e. Logging).

So after this short introduction, what is exactly meant by a conversational state?

Session Bean Conversational State

Suppose you have a Stateless session bean that is used as a counter for application specific action hits, the bean only has one method called increaseCount and another one called getCurrentCount, the bean code looks like the following:

import javax.ejb.Stateless;

@Stateless
public class Counter {

    private int counter;
    public void increaseCount() {
        counter++;
    }    
    public int getCurrentCount() {
        return counter;
    }
}

Now, suppose that we injected this bean into another Singleton session bean to be used to count the hits on the Singleton bean object, the Singleton bean has only one method called doAction and another called one getActionsCount. The code looks like the following:

import javax.ejb.EJB;

public class TheSingletonBean {

    @EJB
    private Counter counter;

    public void doAction() {
        counter.increaseCount();
    }
    public int getActionsCount() {
        return counter.getCurrentCount();
    }
}

Now suppose at some place in the application, we did the following:

@EJB
TheSingletonBean bean;
.
.
.
bean.doAction();
bean.doAction();
bean.doAction();

System.out.println(“Actions Count = “ + bean.getActionsCount());

Now it is supposed that the output on the console become 3 as the method doAction called 3 times and the method increases the counter in each call. But this may be wrong, why, because the Counter Stateless Session Bean maintains no conversational state. Each time the doAction method calls the Counter bean increaseCount method, the container may give TheSingletonBean another Counter bean object, so the output may be 0 or may be 1 or may also be 3, it is all dependent on how the container maintained the Counter Stateless bean objects pool.

But in case we changed the annotation on the class Counter from Stateless to Stateful, the output on the console will be always 3, as the container will always return the same object to the TheSingleton bean.

Like us on Facebook