Stateless Session Bean vs Stateful Session Bean

There are two types of Session Beans. They are:

a)Stateless Session Bean,

b)Stateful session Bean

A stateless Session Bean , as the name implies , does not keep a conversational state with the calling client. In other words , if a stateless session bean has two business methods , and if a client is simultaneously accessing both the methods. Then we cannot guarantee that the state of the bean remains the same. If another client is trying to access the bean , the container may give the existing instance to that client. So the state can be modified by the new client.

In case of a Stateful Session Bean , a conversational state is maintained between the client and the bean. In other words there will be separate bean instances for each client.So state for a client cannot be changed by a new client.

Now let us discuss the life cycles of each type of beans.

Statelful Session Bean Life Cycle

1)In case of Stateful Session Bean , the life cycle starts when the client invokes the bean.

2)If a method with @PostConstruct annotation is there , it will be invoked

3)Now the init() or ejbCreate() method will be invoked,if any. Now the bean is active.

4)Now the container can move the bean to deactivate state or passive state. Passive state means , the bean instance will be moved to secondary storage.If there is a method exists with @PrePassivate annotation , it will be executed before moving the bean to passive state.

5)If a client invokes a bean in existing in passive state , it will go to active state .If a method with @PostActivate annotation is there , it will be executed by the EJB container. The bean will stay in active state.

6)Now if the client invokes the method with @Remove annotation, the method with @PreDestroy will be invoked by EJB container .Now the bean is ready for garbage collection.

Stateless Session Bean Life Cycle

There are only two states in a Stateless Session Bean life cycle .

1)Does not exist state

2)Ready for invocation from client.

The EJB container creates a pool of beans and does the necessary dependency injection. Then , if a method with @PostConstruct annotation is there ,it will be invoked.Now the bean is ready for client invocation.If a method annotated with@PreDestroy is there , then that method will be invoked at the end of client invocation. Now the bean is ready for garbage collection.

Stateless Session Bean vs Stateful Session Bean example

Now we can discuss an example which compares both types of beans. Each bean has two methods. First method sets a String attribute to bean .Second method Retrieves the value of attribute.The client application has two bean references. Client invokes the bean methods simultaneously . In case of Stateless Session Bean , the bean is not keeping a conversational state with client. In case of Stateful session Bean , bean keeps a conversational state with Client. In other words , in case of a Stateful Session Bean, the two bean references from the client are unique. In case of Stateless Session Bean, the same reference can be reused by the EJB container .

2)In eclipse , start a new EJB Project by File–>New–>EJB Project. Give suitable name to the project. In our case , it is given as SessionBeanSample. Select EJB version as 3.x and run time as JBoss 6.x.

3)Now right click on the ejbModules . Then select New –>Package.Give suitable name as package . In this example , we are using com.beans as package name.

4)Right click on the package . The select New–>Session Bean.Select State Type as Stateless.Give the name as StatelessBean.Select both local and remote options for interfaces.

Press Finish.Now the interfaces and class will be created. paste the contents as given.

Default methods in interface is a new feature to Java which was introduced as part of Java 1.8.Till Java 1.8 method implementation in an interface was out of scope.In this new approach, it is possible to implement a method in an interface with the default keyword at the beginning of the method signature.This discussion gives […]

We have already seen the fundamentals of JNDI along with examples for Naming as well as Directory Services using JNDI. So far, all the JNDI examples using LDAP were fetching/updating a particular user from the DIT. In this section we would list all the users from LDAP using JNDI in “ou=users,ou=system” in a generic way. […]