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,