• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Stateful session bean

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks James and Marcofor your help.Your expalinations were very sueful for me.
-Anurag Narayan
 
reply
    Bookmark Topic Watch Topic
  • New Topic