wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes Stateless vs Stateful bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Stateless vs Stateful bean" Watch "Stateless vs Stateful bean" New topic
Author

Stateless vs Stateful bean

vShyam Sundar
Ranch Hand

Joined: Oct 13, 2010
Posts: 60
Hi,
Although this is very old question, I am really in need of help in understanding the difference between stateless and stateful bean.

I have read that stateful beans are used to maintain a conversational state between multiple calls of same client request.

i.e., Stateless session bean is one shot process where as stateful session bean is a multistep process.
Eg of stateful session is flight ticket or hotel ticket booking etc.

Can any one provide me a working example of stateful session bean that holds the conversational state between multiple calls of same client request.

-Shyam
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

The most common example for Stateful session beans is the ShoppingCart. So, the client would repeatedly call, say, addToCart or removeFromCart and the Cart will be maintained for that client (i.e the state (instance variables) that the methods affect will be available only for that particular client).

A better explanation can be found in the tutorial


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
vShyam Sundar
Ranch Hand

Joined: Oct 13, 2010
Posts: 60
Hi Ranganathan,
First of all thanks for your valuable reply, As per my understanding stateful session beans is not with respect to maintaining state on instance variables alone.

Implement a bean with two instance variables with default values, on the first call to bean from a client, update the instance variables with different values. Call the bean again with the same instance that you created first, your instance variable state will be maintained, whereas if you invoke with different instances then you will get the default values.This happens regardless of whether it is a stateless or stateful bean

What I understood from stateful session bean is: It is a kind of replacement for HTTP Session(Not to the full extent), So your stand alone clients can invoke and maintain the state with the bean.

What I am looking for is a working example of stateful session beans.

If you consider an e-commerce application

Step 1 -> User enters their mail address and clicks on next
Step 2 -> User enters their shipping address and clicks on next
Step 3 -> User enters their payment information and completes the purchase.

So my state has to be maintained across all the three pages and My view of Stateful session bean is the same. Let me know if my understanding is wrong.

Thanks,
Shyam.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

vShyam Sundar wrote:Implement a bean with two instance variables with default values, on the first call to bean from a client, update the instance variables with different values. Call the bean again with the same instance that you created first, your instance variable state will be maintained, whereas if you invoke with different instances then you will get the default values.This happens regardless of whether it is a stateless or stateful bean

Wrong. What you said is correct for stateful session beans and wrong for stateless session beans.
vShyam Sundar
Ranch Hand

Joined: Oct 13, 2010
Posts: 60
Hi Ranganathan,
Thanks for your reply. Please go through the below url. one more person has also posted the same question on stack overflow.

Stackoverflow

He has posted the code. Even I face the same problem.

If you have any code with SLSB and SFSB, please do post here.

Thanks,
Shyam.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

My answer remains the same. You can have variables in a stateless session bean, but, that will be shared across all instances of the bean. So, if you lookup a bean, set a value and print it, fine you will get whatever you set. Now, when you do the lookup from say, another program and just print the value, you will get the same value whatever was set by the earlier program's call.
Whereas, in a stateful session bean, with the second call, you will get a fresh instance and the default value for the variable.
vShyam Sundar
Ranch Hand

Joined: Oct 13, 2010
Posts: 60
Hi,
I agree with your point on SFSB,where you always get the default set of values on your instance variables on every look up.But with respect to SLSB when i tried looking up the bean multiple times,I got the default values assigned to instance variables on every look of SLSB, Not the values assigned during previous look up calls. This might be because of the pooling mechanism as i might have at the max looked up two or three bean instances whereas the pool might have more than two instances.

Can you post me any example that differentiates the concepts on SFSB and SLSB?

-Shyam.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

vShyam Sundar wrote:But with respect to SLSB when i tried looking up the bean multiple times,I got the default values assigned to instance variables on every look of SLSB, Not the values assigned during previous look up calls. This might be because of the pooling mechanism as i might have at the max looked up two or three bean instances whereas the pool might have more than two instances.

Bean pool management in this case is handled by the container. So, we shouldn't be worrying about that. Can you post the code you tried?
vShyam Sundar
Ranch Hand

Joined: Oct 13, 2010
Posts: 60
Here is the sample code that i tried




on Clicking submit, control navigates to servlet named ActionServlet



ActionServlet calls business method on a bean named stateful session bean









and finally my ejb-jar.xml




I am getting the same output for stateful and stateless session bean, My assumption is when my bean is defined as stateful, I should get the name during lookup of bean for the second time.

Let me know if I am wrong somewhere.

-Shyam.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

Stateless session bean is good when one method accomplishes completely one task!!!

Statefull session bean is good when to perform one task you need to call several methods of the same instance.
Each subsequent method call relies on the result of previous method call. You keep state among method invocations.

Singleton session bean is good when you need static object per container. One is shared by everyone!
When a bean is a singleton, it simply means that every time you access the bean, you will get a reference to the same object with the same state

Singleton is good to initialize resources, do time-consuming job which can be done when container starts due to @Startup.

My opinion:
Singleton is not good to use when its method is invoked concurrently because it may take much time to perform one method for 100 clients simultaneously.
But stateless bean has pool of its instances. So 100 clients call the same method of 10 instances from pool.
At the same time 10 instances do job faster than 1 instance.


True person is moral, false is right!
vShyam Sundar
Ranch Hand

Joined: Oct 13, 2010
Posts: 60
Hi,
State will be maintained with both stateless and stateful session bean on subsequent calls to bean. you can look at the code that I posted.

With Stateful bean a new instance will always be created whenever a request arrives.

With Stateless bean a instance of the bean will be pulled out from the pool whenever a request arrives.

But my question here is stateful session beans are used to maintain conversational state with the clients, In other words i can say it is similar to HTTPSession for keeping user specific information when accessed from a stand alone clients. If you are aware of this can you post a sample code for the same.

-Shyam.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

Read once more my post first of all. It is written by me from real experts.

Next, read chapter 22 http://docs.oracle.com/javaee/6/tutorial/doc/javaeetutorial6.pdf

Then you may read http://www.jguru.com/faq/view.jsp?EID=917

With Stateful bean a new instance will always be created whenever a request arrives.

is the opposite to
it is similar to HTTPSession for keeping user specific information
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

vShyam Sundar wrote: In other words i can say it is similar to HTTPSession for keeping user specific information when accessed from a stand alone clients. If you are aware of this can you post a sample code for the same.


Your understanding is correct. Your example is fine too. I would just ask you to modify the example a bit - just lookup the bean and display the value and do this only once. You can see how the values are for subsequent requests from the servlet (and check this in case of both stateless and stateful).
vShyam Sundar
Ranch Hand

Joined: Oct 13, 2010
Posts: 60
Hi Ranganathan,

I tried the same example as indicated by you. I could only see

when I declare my bean as stateful, A new bean instance is created for every request/subsequent request(Create method in the bean is called for every request).

Whereas for stateless bean, a bean instance is created once(Create method in the bean is called only once) and for every request/subsequent request the same values are getting printed.

Am i missing something here?


-Shyam.
vShyam Sundar
Ranch Hand

Joined: Oct 13, 2010
Posts: 60
Hi volodymyr,
I agree with you. What i was looking for

I was referring a book(Manning EJB 3 in action) for understanding the stateful bean and on page 51, You could find an image and an explanation. I was looking for an implementation of that kind.

Where there are four steps in completing the application and on each step the user fills in some information and Proceeds to the next step and on final step when user clicks confirm on the final step, the entire process gets completed.

my question here is where the user filled in information exists, in case if it is not a web application? If it is a web app then i can put all my objects in HTTP session.

Let me know if i am wrong some where.

Thanks,
Shyam.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

vShyam Sundar wrote:when I declare my bean as stateful, A new bean instance is created for every request/subsequent request(Create method in the bean is called for every request).
Whereas for stateless bean, a bean instance is created once(Create method in the bean is called only once) and for every request/subsequent request the same values are getting printed.

Am i missing something here?

No, this is the expected behavior. And that is the difference between a stateless and stateful.
vShyam Sundar
Ranch Hand

Joined: Oct 13, 2010
Posts: 60
But how does that help in maintaining a conversational state with the client, In my case a servlet.

As I said before,

Lets Assume, application has 5 screens and each screen collects some information-> how can you have data entered on each screen in stateful bean without persisting the values in RDBMS or having it in HTTP session

Am sure that state cannot be maintained, because the moment you navigate from screen 1 to screen 2, you need to look up your bean again and this creates a new bean instance.

Refer EJB 3 in action book for an example on stateful session bean. There was no implementation in detail.

Let me know if i am lagging somewhere.

-Shyam.
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

I have posted a very similar query as yours (Stateful session bean implementation) and I have exactly the same set of questions you have.

As per my understanding, I was expecting only one stateful session bean to be returned in one session. Hope someone will throw light on this.


Regards,
Prajakta
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

vShyam Sundar wrote: because the moment you navigate from screen 1 to screen 2, you need to look up your bean again

You don't have to lookup the stateful bean again. If you need state across multiple invocations then you use the stateful session bean across calls. Now, where and how you maintain that previously looked up stateful bean is up to the application. The EJB3 spec guarantees that as long as you are invoking on that same stateful session instance, your values in the bean instance will be available across calls.

[My Blog] [JavaRanch Journal]
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

I have just implemented simple example that shows very clearly that scope of Stateful bean depends on scope of client that uses it.
It means that if Stateful Bean client (like ManagedBean) has scope of view (one page scope) then Statefull bean itself will have exactly the same scope - the same instance exists only per page. And if you want your Stateful Bean to keep state( be the same instance) among multiple requests you should inject it into Session Scoped client. Looking at my demo I suggest that Stateful Bean scope is scope of this Stateful bean client.

With Stateless bean the situation is that it is created really only once. It is created when it is used for the first time. Stateless bean is created only once during all its lifecycle. After Stateless bean method finishes it is returned to the pool with all changes that you made in that method. When you use Stateless bean for the second time previously created instance is returned to the client from the pool or new one is created and after it's method is finished it is put into pool. Only container completely controls lifecycle of stateless bean and user can't anyhow remove or create it! I don't think that always the same instance from pool is returned to the same client. Therefore Stateless bean is good to perform completely one task for one method invocation.

Finally I recommend this link - http://www.datadisk.co.uk/html_docs/ejb/ejb3_session_beans.htm
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

vShyam Sundar wrote:Whereas for stateless bean, a bean instance is created once(Create method in the bean is called only once) and for every request/subsequent request the same values are getting printed

It's just a co-incidence and server specific implementation detail that you are getting the same SLSB instance for multiple invocations. It's totally upon the server implementation to pool the instance (which is what is happening here) or use a new instance every time you invoke on the SLSB proxy.
vShyam Sundar
Ranch Hand

Joined: Oct 13, 2010
Posts: 60
Hi All,
To summarize my understanding, Stateless session beans are not guaranteed to maintain the client specific state(because pooling strategy is controlled by the container) and stateful session beans are guaranteed to maintain the client specific state

As suggested by Jaikiran :

"You don't have to lookup the stateful bean again. If you need state across multiple invocations then you use the stateful session bean across calls. Now, where and how you maintain that previously looked up stateful bean is up to the application. The EJB3 spec guarantees that as long as you are invoking on that same stateful session instance, your values in the bean instance will be available across calls. "


So if it is a web application, I have to maintain the stateful bean instance in the HTTP session right? and what about native or stand alone applications?
Any ideas?

Thanks Volodymyr for your valuable replies.

Thanks,
Shyam.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

vShyam Sundar wrote:So if it is a web application, I have to maintain the stateful bean instance in the HTTP session right? and what about native or stand alone applications?

Standalone applications can simply maintain the variable as an instance variable. In web applications, I think we need to store the instance in HttpSession
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stateless vs Stateful bean