Stateless session beans or stateful session beans (Java )

Asked By 250 points N/A Posted on -
qa-featured

In EJB, there a several bean types in the Session Bean Type category.

They are stateless and stateful. Which type should I choose for a new EJB project?

SHARE
Best Answer by TekGirl
Answered By 5 points N/A #92010

Stateless session beans or stateful session beans (Java )

qa-featured

Stateless Session beans in EJB specification denotes objects that do not require state management from the EJB container. This means the container does not have to worry about transactions or state management of the object.

When you take a Stateful Session bean, the EJB container has to worry about the state of the object and atomicity of its transactions. That is a Stateful session bean imposes a considerable overhead on the EJB container. If you can explain your intended project we would be able to help you on what to choose.

Because the use of a Stateful or a stateless depends on the context it is going to be used.

Answered By 0 points N/A #92012

Stateless session beans or stateful session beans (Java )

qa-featured

Stateless beans are more preferred, as the same instance can be distributed among many client calls.

A new object is only created if the existing method call is still active. i. e. the EJB container checks if a method call is in progress. If not the object is given to client A. When client B comes along, the container looks if Client A has finished. If so, without destroying or resetting the object, the same object is given to client B.

This means that the overhead of creating new objects for every method call is reduced. A single object instance can service many requests. The drawback here is that the bean properties, if any, are not reset. Therefore in event the object contains a private variable, the value of which is accessible to all clients.

Answered By 250 points N/A #92014

Stateless session beans or stateful session beans (Java )

qa-featured

The intended application is an inventory control system. We are using EJB3 for persistence. For the business logic, we are checking if we are to use EJB session object or if we should just use the managed beans in the web project.

I need to know what type of a bean to use if we are to move the business logic to the EJB project.

Answered By 0 points N/A #92015

Stateless session beans or stateful session beans (Java )

qa-featured

I would advise you to use Stateless session beans for all business related methods. If you compare the number of web managed bean projects created to the number of stateless session beans created to service 100 clients. you will see the ratio more or less 100 : 2 !. This is a drastic improvement in performance and less memory usage.

Please bear in mind that the methods need to be internally cohesive, instead of reliance on class level variables. This is because you cannot guarantee that the same instance will be returned to you on subsequent method calls.

Best Answer
Best Answer
Answered By 5 points N/A #92016

Stateless session beans or stateful session beans (Java )

qa-featured

The following code will help you understand the pitfalls of a stateless session bean, with class level variables:

@Stateless
public class PaymentController implements PaymentControllerRemote, PaymentControllerLocal {

    private int numberOfCalls = 0;
    private String _cardNo = "";
    private String _cardName = "";
 
    public String submitPayment(String cardNumber, String cardName) {
        this._cardNo =  cardNumber;
        this._cardName = cardName;
 
        numberOfCalls +=1;
        
        return "OK: " + cardNumber + cardName;
    }
   
    public String getCardNo() {
     return _cardNo;
   }
 
}
 
Here the card number and card name is stored in a class level variable. Client A calls the submit Payment method passing the card number and card name. Client B calls the getCardNo method.  As the object is marked as a stateless session bean, the EJB container passes the cached object to Client B "as-is". This means client B can "see" what Client A had put!
 
Therefore, you should avoid class level variables when using stateless session beans.
Answered By 250 points N/A #92017

Stateless session beans or stateful session beans (Java )

qa-featured

I understand the danger of using a stateless session bean with class level variables.

How about Stateful session beans?

When should I use them ?

Answered By 0 points N/A #92018

Stateless session beans or stateful session beans (Java )

qa-featured

Stateful Session Beans are used, if you are using resources that require to be thread safe. In the event you are accessing database related records, its is more prudent to use stateful session bean objects. When you take the manager class that manages the entity objects, it is usually termed as statefull session bean.

This is because when you modify a record in the database, you do require a transaction.

Furthermore the code, TekGirl could instantly made safe by changing the @Stateless to @Stateful.

Answered By 5 points N/A #92019

Stateless session beans or stateful session beans (Java )

qa-featured

Actually you could still use a stateless session bean and manipulate the database using the entity manager factory. Following is an example of the same.

@Stateless
public class OrderManager implements OrderManagerRemote {
 
    @PersistenceContext(unitName="CentralLogicPU")
    private EntityManager em;
    
 
    public CustomerOrder saveOrder(CustomerOrder orderData) {
       if (orderData.getId() == null) {
        em.persist(orderData);
       } else {
        em.merge(orderData);
       }
      
       return orderData;
    }
 
As you can see the EntityManager is a class level variable used by all method calls. This is perfectly safe as the "em" variable does not pose a threat to business data.  But if you want complete protection at expense of performance, you can go stateful. Banking applications are normally on the stateful side.
Answered By 250 points N/A #92020

Stateless session beans or stateful session beans (Java )

qa-featured

Thank you TekGirl and DimaZ.

You all helped me to understand which type of a session bean to use on which type of a situation! It was a great learning for me!

Thank you again!

Related Questions