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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Stateful EJB internal mechanism" Watch "Stateful EJB internal mechanism" New topic
Author

Stateful EJB internal mechanism

JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Hi gurus,
Can someone explain to me how a stateful EJB recognizes the client that has instanciated it ?
I mean, in a web app, the client is a Http browser (stateless).
It uses a stateful EJB, so it means the EJB is able to "recognize" the client each time a different method is called.
I can't figure out how this information is stored and managed.
So what I really interested in is the EJB container behavior.

Thanks in advance for your help.


/ JeanLouis<br /><i>"software development has been, is, and will remain fundamentally hard" (Grady Booch)</i><br /> <br />Take a look at <a href="http://www.epfwiki.net/wikis/openup/" target="_blank" rel="nofollow">Agile OpenUP</a> in the Eclipse community
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16141
    
  21

I'm not sure what you mean. The "state" in a stateful EJB is due to the fact that there are multiple instances of that class, and the internal properties don't change between references of the bean. Neither the bean nor its container "recognize" the client, instead the client has a handle on a particular bean instance and doesn't let go until its done using that bean instance.
By contrast, a stateless bean isn't permitted to have (or at least depend) on internal properties of a particular instance of the bean -- every instance must act identically. Because of this, you have no need to grab a bean and hold onto it, you just get one from the pool, use it and release it. This way you can often get by with fewer bean instances as well as avoid the construction and destruction processes, since stateless beans can safely be recycled without worrying about what their insides were set to by the last client.


Customer surveys are for companies who didn't pay proper attention to begin with.
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Hi Tim,
You exactly pinpointed my problem.
How does the client "manage" that handle on the Stateful EJB ?
To be able to use the same EJB instance each time, the browser has to keep this handle somewhere.....Quite mysterious for me.
Nitin Shivaram
Ranch Hand

Joined: Jan 17, 2001
Posts: 71
Hi Bill,
Holding a reference to the stateful bean would not be the done by the browser but by a server side application which runs in a browser, typically a servlet or a jsp.
This is how you could do it.
The reference to the stateful session bean has to be converted to an javax.ejb.Handle, to make sure you abstract the non-bean characteristics, and then store this handle in a session.
And then when you have to use it, retrieve from the session, call getEJBObject() to get the reference to the stateful bean.

------------------
Nitin S
Sun Certified and Tekmetrics Certified Java Programmer For the Java 2 Platform.


Nitin S<br />Sun Certified Java Programmer for the Java 2 platform.<br />Tekmetrics Certified Java Programmer For the Java 2 Platform.
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Thanks Nitin.
This explanation is very clear and very obvious but ...
Imagine I can't use cookie nor urlRewriting to manage session tracking because of my clients (Soem wap devices do not support it).
Then the way out to manage session tracking is to use... Stateful Session Beans... So I'm stuck with your solution.... I can't store the handle anywhere...
Originally posted by Nitin Shivaram:
The reference to the stateful session bean has to be converted to an javax.ejb.Handle, to make sure you abstract the non-bean characteristics, and then store this handle in a session.


[This message has been edited by Bill Bailey (edited November 30, 2001).]
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Bill -- stateful session beans won't help you. They can't help you. If you can't use cookies or URL rewriting there is NO way to maintain client state. There's nothing magic about a stateful session bean. It's just like any other stateful java object -- you've got to store the reference somewhere. 90% of the time that "somewhere" is in either a cookie directly or in the HttpSession keyed by a cookie or rewritten URL.
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Thanks to all for these clarifications.
Laurent Raynal
Greenhorn

Joined: Jul 20, 2001
Posts: 12
Hi,
I'm a bit confused about what to keep on the web tier to have a handle on the statefull session bean (SFSB) "Client Controller". I use Sun's Petstore 1.1.2 app as a tutorial, and what I see is that the reference kept in the HTTPSession is the EJB's remote reference (simply the return of the create() method).
I applied the same architecture in my app : I use a SFSB as a client controller to maintain state. However, I noticed that the SessionContext principal isn't maintained between HTTP requests (it is lost on the second request, the first one after creating the SFSB). My servlet is unauthenticated, all authentication is done on the SFSB. It seems that web unauthentication is transmitted to the EJB tier. Am I right ? I'm using WL 5.1 .
Thanks
Originally posted by Nitin Shivaram:
Hi Bill,
Holding a reference to the stateful bean would not be the done by the browser but by a server side application which runs in a browser, typically a servlet or a jsp.
This is how you could do it.
The reference to the stateful session bean has to be converted to an javax.ejb.Handle, to make sure you abstract the non-bean characteristics, and then store this handle in a session.
And then when you have to use it, retrieve from the session, call getEJBObject() to get the reference to the stateful bean.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Stateful EJB internal mechanism