aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Comparing stateful session bean with stateless session bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Comparing stateful session bean with stateless session bean" Watch "Comparing stateful session bean with stateless session bean" New topic
Forums: EJB Certification (SCBCD/OCPJBCD) EJB and other Java EE Technologies
Author

Comparing stateful session bean with stateless session bean

Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

Hi All,

I know this has been quite a repititive topic that we come across in forums. Still, I want to summarize my understanding about them and want to discuss a doubt which I am unable to clear till now.

My understanding:
A] Stateful beans
1) Every lookup of stateful session bean returns a new instance of the bean. It is the developer's responsibility to associate this new instance with a user by storing it in user's session.
(May be we can look up a stateful bean on creation of new session by implementing SessionListener interface)
2) After retrieving stateful session bean from session (HttpSession), it is guaranteed that the state of the bean is unchanged.

B] Stateless Beans
1) On looking up stateless bean, user might get some random instance of the bean, available in server's pool.
As such, the bean's state cannot correspond to a client since one client might get an instance meant for another client. So, stateless beans are not used where state is involved.
They are used only for business methods implementation.
2) So, after retrieving stateless session bean from session (HttpSession), it is not guaranteed that the state of the bean is unchanged.

Now below is my confusion:
1) I wrote a servlet which gives call to a BusinessDelegate to lookup EJBs (Stateless and Stateful both)
Everytime user clicks on 'Next' button, lookup happens.
2) For stateless bean , I always get only one instance while for stateful, I always get a new instance.
3) After repeating above step for various number of times, step 2 result is the same. I never get a new/randomly picked instance of the stateless bean.
4) Also, I am invoking some method on the stateless bean in loop. But the state of the bean is maintained across method calls. (This contradicts the statement that state of stateless bean is not maintained across method calls).
5) I tried storing the stateless bean in session and accessing it later, still the state of the bean is maintained properly.

Below is my code.
1) Servlet/Controller


2) BusinessDelegate (Where lookup of beans is done)


3) AccountBean (Stateless Bean)


I am struggling to understand this , any inputs would be highly appreciated!!!

Thanks,


Regards,
Prajakta
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1734
    
  25

Hi Prajakta Acharya,

1) I wrote a servlet which gives call to a BusinessDelegate to lookup EJBs (Stateless and Stateful both)

Hmm, I don't see a Servlet in your code (@Controller, Spring bean ?). So I guess there is only one Controller, right? why don't you code a Servlet to see if you understanding is correct?

For stateless bean , I always get only one instance

Are you sure, what makes you think like that? Can you add a @PostConstruct method to your EJB and see how many times it is called. Did you try using another browser to see whether you get another instance? By the way: the container can decide to use the same instance if the same instance is available, so if you invoke the methods after one another it is very well possible.

for stateful, I always get a new instance.

How do you know this? Do you keep a reference to the stateful bean in the session object?

Also, I am invoking some method on the stateless bean in loop. But the state of the bean is maintained across method calls.

That the state is kept is possible, but you should rely on state being kept on a stateless session bean: so don't do it.

Regards,
Frits
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

Hi Frits,

Firstly thank you for your response.

1) Yes, the code that I have posted is of a Spring Controller.
Earlier, I had written a Servlet and EJB was injected to it using @EJB annotation.
But I switched to a Spring Controller and a business delegate because on every hit of 'Next' button I wanted to look up the beans and not inject them once in the Servlet.

2) Regarding same instance of stateless bean being returned, I am sure because I have an instance variable of the bean which increments every time I invoke 'deposit' method of the bean. (Please refer AccountBean and BusinessDelegate code). After looking uo the bean every time and invoking 'deposit' method, the account instance variable keeps on incrementing sequentially, for all the testing cycles.
I tried with @PostConstruct. It gets called only once.

I have tested this with 2 browsers but get only single instance always.

3) For the stateful bean, I had written a @PostConstruct callback which gets called for every lookup. Posting that code below.


On maintaining the stateful/stateless bean in HttpSession, on retrival, I get reference to the same instance which was stored. (This is true for stateless bean as well).

So was wondering why is it said that stateless beans dont maintain the state....

Thanks,
Prajakta

Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1734
    
  25

Hi Prajakta,

I tried with @PostConstruct. It gets called only once.

Ok, this gives you a hint: the container only instantiates one Stateless Session Bean, this means that all the requests will be handled by the same instance as long as that one is available for the next request (i.e. not still busy with the current request). Is there a container setting that the initial pool of SSB's should be as big as one? What Application Server are you using? Normally, if the pool of Stateless Session Beans can handle all the requests sequentially there is no need to instantiate another Bean. I assume that you fire the requests from one machine after-one-another? If you are able to fire request simultaneously from different clients you can see whether the container instantiates another bean (or throws an Exception)

For the stateful bean, I had written a @PostConstruct callback which gets called for every lookup. Posting that code below.

Yes, there is no pool of Stateful Session Beans, so this works as designed. The fact that there should be one per client is something you should take care of (by storing a reference in the Session for example)

So was wondering why is it said that stateless beans dont maintain the state....

Be careful here, from the EJB specs: "The term 'stateless' signifies that an instance has no state for a specific client." and "The Bean Provider must exercise caution if retaining any application state across method calls."

The container can grab any instance from the pool to serve the request. In other words, if two requests are fired after one-another, you as a Bean Provider will never know if you will get the same instance. SSB's can contain state (i.e. db-connection), but should not contain state for one client.

Regards,
Frits
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

Hi Frits,

I must say your explaination is crystal clear. I got the picture clearly now.
I tried this demo on two servers: TOMEE and JBoss and got same results.
Tried to look at the configuration file of TOMEE which holds stateful/stateless session bean configurations (tomee.xml) but could not get to such config of pool size.

I now have a subquestion regarding this :
If we need to associate a stateful session bean to user by using HttpSession, what makes stateful bean different than any other object which can be stored in session?
While retrieving a bean from session, session management will still have to be done by the server.
Seperating business logic can anyway be done by stateless beans.



Thank you once again for your inputs;I was struggling since few days, trying to simulate scenarios to understand this.

Regards,
Prajakta
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1734
    
  25

Hi Prajakta,

Be careful with the Edit-button, I just saw that you edited your post after I had read your reply. Next time just post another message,ok?

what makes stateful bean different than any other object which can be stored in session?

EJBs are not like simple POJO's, the EJB container adds functionality to your EJB's like remote access, security, concurrency, transactions, and so on. This way you can concentrate on your business logic and let the container handle the other features.

Regards,
Frits
Prajakta Acharya
Ranch Hand

Joined: Nov 08, 2012
Posts: 138

Ok...got it
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparing stateful session bean with stateless session bean