aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Stateful session bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Stateful session bean" Watch "Stateful session bean" New topic
Author

Stateful session bean

Anurag Narayan
Ranch Hand

Joined: Jul 14, 2008
Posts: 41
What exactly do we mean when we say that stateful session bean maintaines the session? Does it mean that,like in servlets a value can be found throughout the time I have opened the browser?What will happen if I instantiate a stateful session bean, set a value,goes to some other bean in the application,comeback to the same bean;insantiate it again;will I get the value of the variable I set up last time?


Anurag Narayan<br />SCJP5,SCEA(OCMJEA)
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Anurag,

that's correct. With stateful session beans you tell the EJB container to maintain state of such beans and the container routes every subsequent client request to the same bean instance.

So you can say it's like HTTP sessions and servlets. How exactly the container manages the sessions is an implementation detail you don't have to worry about. The clients of an EJB stateful bean get a session by requesting a bean stub via JNDI lookup and they use this corresponding bean instance until the session expires or the bean is explicitly removed. I'm unsure if the session is determined the same way when you acquire a stateful session bean from another bean via dependency injection (with annotations). But I think it will be similar in this case because the other bean then acts as a client, too.

Hope this helps for your understanding...

Marco
[ September 28, 2008: Message edited by: Marco Ehrentreich ]
Anurag Narayan
Ranch Hand

Joined: Jul 14, 2008
Posts: 41
Hi Marco,
Thanks for your reply.One thing I would like to clarify though.In servlets if I set a value say x=10 in session in a servlet A, I could find that value x=10 in another servlet B.
In bean also is this thing possible?Like, if I set a value x=10 in stateful session bean A,I leave this bean and move to another stateful session bean B,while I am in the same session will I find x=10 in bean B which I had set in bean A?Or will I find that value in bean A only?
Thanks,
Anurag Narayan
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Anurag,

I hope I can explain what you want to know... So if you want to use a session bean (stateless or stateful) you usually request an instance of this bean from the EJB container either by looking it up via JNDI or with annotations and dependency injection. If a bean with this lookup name exists you'll get an instance of that bean from the container.

STATELESS session beans are managed in a pool and you can't expect to get a specific instance because all stateless session beans are reused between all clients.

If you request a STATEFUL session bean the EJB container establishes a session which is specific to exactly one client. Unfortunately I can't find any detailed information on how the container determines what a "client" is in this context. Anyway the container creates a session and an instance of the stateful session bean which is given to the client. And the same client within the same session always gets the one and only single instance of a stateful session bean. So a value x=10 in bean A will still be there in a subsequent request from the same client in the same session.
If I understand your question correctly you want to know if a second bean B of the same class as bean A would have the same value x=10?!? But this is something which will never happen. If you use a stateful session bean and you once have established a session you will always get the same single instance of this bean from the container. So the container simply wouldn't create two instances A and B of a stateful bean of the same class for the same client.

Is it that you wanted to know? If not just let me know and I'll try to give you more information

Marco
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
I hope I can explain what you want to know... So if you want to use a session bean (stateless or stateful) you usually request an instance of this bean from the EJB container either by looking it up via JNDI or with annotations and dependency injection. If a bean with this lookup name exists you'll get an instance of that bean from the container.


Just to clarify, an EJB container never provides an instance of the bean from the container. The container either provides a reference to either the local or remote interface of the EJB. EJB clients only get a reference to the interface of the bean, never the bean itself.
[ October 06, 2008: Message edited by: Scott Selikoff ]
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Just to clarify, an EJB container never provides an instance of the bean from the container. The container either provides a reference to either the local or remote interface of the EJB. EJB clients only get a reference to the interface of the bean, never the bean itself.

Sorry, this was in fact imprecisely explained by me. Of course the client only deals with the interface references it gets from the container like James said.

Marco
Anurag Narayan
Ranch Hand

Joined: Jul 14, 2008
Posts: 41
Thanks James and Marcofor your help.Your expalinations were very sueful for me.
-Anurag Narayan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stateful session bean