Difference Between Stateful and Stateless Session Beans

By: Paul Allen and Joseph Bambara Emailed: 1608 times Printed: 2077 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Session beans can either be stateful or stateless. With stateful beans, the EJB container saves internal bean data during and in between method calls on the client’s behalf. With stateless beans, the clients may call any available instance of an instantiated bean for as long as the EJB container has the ability to pool stateless beans. This enables the number of instantiations of a bean to be reduced, thereby reducing required resources.

Stateless Session Beans

A session bean represents work performed by a single client. That work can be performed within a single method invocation, or it may span multiple method invocations. If the work does span more than one method, the object must retain the user’s object state across the method calls, and a stateful session bean would therefore be required.

Generally, stateless beans are intended to perform individual operations automatically and don’t maintain state across method invocations. They’re also amorphous, in that any client can use any instance of a stateless bean at any time at the container’s discretion. They are the lightest weight and easiest to manage of the various EJB component configurations.

Stateful Session Beans

Stateful session beans maintain state both within and between transactions. Each stateful session bean is therefore associated with a specific client. Containers are able to save and retrieve a bean’s state automatically while managing instance pools (as opposed to bean pools) of stateful session beans.

Stateful session beans maintain data consistency by updating their fields each time a transaction is committed. To keep informed of changes in transaction status, a stateful session bean implements the SessionSynchronization interface. The container calls methods of this interface while it initiates and completes transactions involving the bean.

Session beans, whether stateful or stateless, are not designed to be persistent. The data maintained by stateful session beans is intended to be transitional. It is used solely for a particular session with a particular client. A stateful session bean instance typically can’t survive system failures and other destructive events. While a session bean has a container-provided identity (called its handle), that identity passes when the client removes the session bean at the end of a session. If a client needs to revive a stateful session bean that has disappeared, it must provide its own means to reconstruct the bean’s state.

Stateful vs. Stateless Session Beans

A stateful session bean will maintain a conversational state with a client. The state of the session is maintained for the duration of the conversation between the client and the stateful session bean. When the client removes the stateful session bean, its session ends and the state is destroyed. The transient nature of the state of the stateful session bean should not be problematic for either the client or the bean, because once the conversation between the client and the stateful session bean ends, neither the client nor the stateful session bean should have any use for the state.

A stateless session bean will not maintain conversational states for specific clients longer than the period of an individual method invocation. Instance variables used by a method of a stateless bean may have a state, but only for the duration of the method invocation. After a method has finished running either successfully or unsuccessfully, the states of all its instance variables are dropped. The transient nature of this state gives the stateless session bean beneficial attributes, such as the following:

  • Bean pooling Any stateless session bean method instance that is not currently invoked is equally available to be called by an EJB container or application server to service the request of a client. This allows the EJB container to pool stateless bean instances and increase performance.

  • Scalability Because stateless session beans are able to service multiple clients, they tend to be more scalable when applications have a large number of clients. When compared to stateful session beans, stateless session beans usually require less instantiation.

  • Performance An EJB container will never move a stateless session bean from RAM out to a secondary storage, which it may do with a stateful session bean; therefore, stateless session beans may offer greater performance than stateful session beans.

Since no explicit mapping exists between multiple clients and stateless bean instances, the EJB container is free to service any client’s request with any available instance. Even though the client calls the create() and remove() methods of the stateless session bean, making it appear that the client is controlling the lifecycle of an EJB, it is actually the EJB container that is handling the create() and remove() methods without necessarily instantiating or destroying an EJB instance.


EJB Home | All EJB Tutorials | Latest EJB Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(8)


1. View Comment

i want every important questions in .net 3.5 and java which ask in interview question so please provide me some very intelligent question along with answer

View Tutorial          By: pawan at 2011-07-28 06:17:25
2. View Comment

thank you
well explained


View Tutorial          By: Alexandre Ogrodovski at 2012-12-10 21:13:37
3. View Comment

@pawan: Wow. Your comment is museum-quality art, as it is worthy of expensive and decorative framing, but for all the wrong reasons

View Tutorial          By: Steve at 2013-02-21 20:30:47
4. View Comment

I have some questions : 1) Which type of GC used in SUN Java VM
2)In which kind of IPC mechanism, the chance of race condition is high?
3)Among Stateless and Stateful beans , which is useful interms of Performance testing of an application?


View Tutorial          By: SP at 2013-06-04 15:48:59
5. View Comment

This is very useful article with clear understanding...

Thanks and keep it up!
:-)


View Tutorial          By: Manu Manker at 2014-03-14 05:49:26
6. View Comment

This explanation is simple and very useful.

View Tutorial          By: kiran at 2015-02-22 20:47:13
7. View Comment

When stateless bean not maintains any state and not mapped to client why it is called session bean.:)

View Tutorial          By: Mallikarjun at 2015-09-18 15:42:46
8. View Comment

I came to know about the basic concepts here but I want the clear idea by example.
Wherever I see, I only find that stateless bean does not use class level variable (instance variable) and stateful bean uses the instance variable.
And I also found that there is no relation of session bean with any kind of Http Session.
Where can I get real life example showing the exact thing what these both beans are doing...
Please let me know about the same.


View Tutorial          By: Amit Gorvadiya at 2015-09-23 06:18:37

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Paul Allen and Joseph Bambara
Managing Security in EJB
Difference Between Stateful and Stateless Session Beans
Difference Between Session and Entity Beans
Required Classes/Interfaces That Must Be Provided for an Enterprise JavaBeans Component

More Tutorials in EJB
EJB 3 Entities
Web services basics
Required Classes/Interfaces That Must Be Provided for an Enterprise JavaBeans Component
Difference Between Session and Entity Beans
Difference Between Stateful and Stateless Session Beans
Managing Security in EJB
EJB Entity Beans
Types of EJB

More Latest News
Most Viewed Articles (in EJB )
Difference Between Stateful and Stateless Session Beans
Types of EJB
Required Classes/Interfaces That Must Be Provided for an Enterprise JavaBeans Component
Difference Between Session and Entity Beans
EJB 3 Entities
EJB Entity Beans
Managing Security in EJB
Web services basics
Most Emailed Articles (in EJB)
Types of EJB
Difference Between Stateful and Stateless Session Beans
EJB Entity Beans
Difference Between Session and Entity Beans
Required Classes/Interfaces That Must Be Provided for an Enterprise JavaBeans Component
Managing Security in EJB
Web services basics
EJB 3 Entities