File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes How do clients always end up with the same Stateful bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How do clients always end up with the same Stateful bean" Watch "How do clients always end up with the same Stateful bean" New topic
Author

How do clients always end up with the same Stateful bean

Mark Garland
Ranch Hand

Joined: Nov 11, 2006
Posts: 226
Hi,

With Stateful beans, the client is always redirected to the same bean (or a bean representing the same state).
My question is how?

I guess it must be like a session id in Web development... but I wondered how it worked?
Is this handled at the RMI-IIOP level, or does the stub keep hold of it? I've not see it defined in the client code (nor should it I guess).

Thanks,

MG


28/06/06 - SCJP - 69%, 05/06/07 - SCWCD - 92%, 28/02/08 - IBM DB2 v9 Fundamentals (Exam 730) - 87%, 18/11/08 - IBM DB2 v9 DBA (Exam 731) - 89%, 26/02/11 - SCBCD - 88%
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Because containers don't use instance pooling for Stateful Session Beans.

Actually, it's very normal, for example outside EJB context if clients use Object A, it will use the same object every time.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Mark Garland
Ranch Hand

Joined: Nov 11, 2006
Posts: 226
Hi Kengkaj,

Thank you for your reply.

What I meant by my original question, is how is a client identified by the container?
i.e. a 2nd request comes in from a client - how does the container know which object is theirs?
There must be some id somewhere.

Thanks,

MG
Dhiren Lodhia
Greenhorn

Joined: Mar 05, 2008
Posts: 14
Mark Garland wrote:Hi Kengkaj,

Thank you for your reply.

What I meant by my original question, is how is a client identified by the container?
i.e. a 2nd request comes in from a client - how does the container know which object is theirs?
There must be some id somewhere.

Thanks,

MG


Hi Mark,

I think i can explain your quey.
First thing to remember when using Stateful SessionBean [SFSB] is that there will be unique ONE SFSB for each client.

Now while implementing SFSB there are actually 2 ID that work togather.
First in web containr you create Session and you add the reference to SFSB in the session .
so 1st you have Session id in web container.
now for each session id in web container your EJB container will create a uniwue SFSB and it will also be given some id
and this both Session id & SLSB id are bound togather
so from next time onwards if you come come with same session id you will be given same corresponding SFSB.

try this if your session expires in Web container you will also loose your SFSB.


Dhiren Lodhia
SCJP 5.0 (93%)
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10102
    
165

Mark Garland wrote:
With Stateful beans, the client is always redirected to the same bean (or a bean representing the same state).
My question is how?



Remember the jndi-lookup The lookup returns a proxy and if you keep working on the same proxy, the containers are smart enough to use the same bean instance for a SFSB. So if you do another lookup for the SFSB, the proxy returned will work on a different instance of the SFSB.

[My Blog] [JavaRanch Journal]
Mark Garland
Ranch Hand

Joined: Nov 11, 2006
Posts: 226
Hi Dhiren and Jaikiran,

Thank you very much for your replies.

Dhiren - I understand what you mean by putting a reference to the SFSB into the session, however this is just the same as me holding the reference locally. I'm not sure that this entirely answers my question.

Jaikiran - Are you saying that the JNDI lookup returns a proxy, and for a SFSB each instance of the proxy points to one and only one SFSB? If this is right, it is here where the id is kept.

Thanks once again,

MG
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10102
    
165

Mark Garland wrote:
Jaikiran - Are you saying that the JNDI lookup returns a proxy, and for a SFSB each instance of the proxy points to one and only one SFSB? If this is right, it is here where the id is kept.



Yes.
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Jaikiran Pai wrote:
Mark Garland wrote:
Jaikiran - Are you saying that the JNDI lookup returns a proxy, and for a SFSB each instance of the proxy points to one and only one SFSB? If this is right, it is here where the id is kept.



Yes.

I don't quite sure about this, in that case how the container separate clients? Could you please to explain more?
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Mark Garland wrote:
Dhiren - I understand what you mean by putting a reference to the SFSB into the session, however this is just the same as me holding the reference locally. I'm not sure that this entirely answers my question.

As I understand, we put SFSB to HttpSession to able to use the same SFSB in the session.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do clients always end up with the same Stateful bean