Stateless session beans or stateful session beans (Java )
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?
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?
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.
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.
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.
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.
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 {
I understand the danger of using a stateless session bean with class level variables.
How about Stateful session beans?
When should I use them ?
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.
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.
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!