aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Access to EJBObject from within ebjCreate of a stateless SessionBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Access to EJBObject from within ebjCreate of a stateless SessionBean" Watch "Access to EJBObject from within ebjCreate of a stateless SessionBean" New topic
Author

Access to EJBObject from within ebjCreate of a stateless SessionBean

Kev Miller
Greenhorn

Joined: Jul 16, 2004
Posts: 2
Hi,
on page 227 (Head First EJB) it says that ejbCreate is invoked by the container without a client request. On the following page, however, it says that you can get a reference to your EJBObject from within ejbCreate. How can you get such a reference when the container invokes ejbCreate without any client request? Isn't an EJBObject instance only created when the client invokes create on his home object stub (whereas the EJBObject instance is not linked to an actual bean instance until a business method call)?
Thanks,
Kev
Ana Nava
Greenhorn

Joined: May 25, 2004
Posts: 24
Originally posted by Kev Miller:
Hi,
on page 227 (Head First EJB) it says that ejbCreate is invoked by the container without a client request. On the following page, however, it says that you can get a reference to your EJBObject from within ejbCreate. How can you get such a reference when the container invokes ejbCreate without any client request? Isn't an EJBObject instance only created when the client invokes create on his home object stub (whereas the EJBObject instance is not linked to an actual bean instance until a business method call)?
Thanks,
Kev


I tried to figure out how and but could not come up with an answer for Kev's question. Any one..any explanation for this?
vu lee
Ranch Hand

Joined: Apr 19, 2005
Posts: 189
on page 227 (Head First EJB) it says that ejbCreate is invoked by the container without a client request. On the following page, however, it says that you can get a reference to your EJBObject from within ejbCreate. How can you get such a reference when the container invokes ejbCreate without any client request? Isn't an EJBObject instance only created when the client invokes create on his home object stub (whereas the EJBObject instance is not linked to an actual bean instance until a business method call)?

I don't remember the context on page 227, but I'll attempt to answer your question. Unlike entity bean and SFSB, the ejb container will invoke SLSB ejbCreate when creating a pool of SLSB.

...it says that you can get a reference to your EJBObject from within ejbCreate. How can you get such a reference when the container invokes ejbCreate without any client request?


It means you as a bean developer can get a reference to the EJBObject
Ana Nava
Greenhorn

Joined: May 25, 2004
Posts: 24

It means you as a bean developer can get a reference to the EJBObject[/QB]


So what I infer from this is that for every Stateless Session Bean in the pool there is corresponding EJBObject/EJBLocalObject created by the container to which a bean can get reference to. That doesn't sound right to me.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Nope, it ain't right. What happens is that the creation of the EJBObject is tied to the creation of the Home object. It is only when the client invokes a business method on the EJBObject's stub that the container will pull a bean instance from the pool and associate the instance with its SessionContext and EJBObject by invoking the bean's setSessionContext() and ejbCreate() methods.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Adri Smith
Ranch Hand

Joined: Jun 07, 2004
Posts: 76
I was wondering exactely how it was possible too because you don't have any clients at this point.

And I have made a search on this forum and found a good post about it. In this post you have the response formulated by Kathy Sierra.

Here is the link:

http://www.coderanch.com/t/158485/java-EJB-SCBCD/certification/beanness-stateless-session-bean

Cheers,
Adri
Lahiru Abeydeera
Ranch Hand

Joined: Nov 05, 2004
Posts: 44
I'm also having the same question. not only kathey says that. In valentinez cheat sheets also it says you can get reference to ejb object in a stateless beans session object. And the best thing is one mock exam (Soft SCBCD or www.ejbcertificate.com ) question said you cannot do this. So i'm also confused.
but I can still guess of something possible, which is similar to Roger Chung-Wee's suggestion. I think the container does not execute the ejbCreate the bean instance in the actual creation but when the client calls a buisness method.
Vijay Dasari
Greenhorn

Joined: May 27, 2005
Posts: 9
For Stateful Session beans-
we know that the client maintains the life cycle of the bean, so we can have access to EJBObject in ejbCreate() method.

For Stateless Session Beans:-
The Spec says that "To the client, it appears as if the client controls
the life cycle of the session object. However, the container handles the create and remove calls without necessarily creating and removing an EJB instance".

So we can access the EJBObject in the ejbCreate() of the Stateless Session bean.

NOTE:
isIdentical(EJBObject ejbObject) will always return true for Stateless Session beans.
Ana Nava
Greenhorn

Joined: May 25, 2004
Posts: 24
Thanks Adri and Roger ... now it is clear.

Quote from Vijay Dasari

However, the container handles the create and remove calls without necessarily creating and removing an EJB instance".

So we can access the EJBObject in the ejbCreate() of the Stateless Session bean.


Vijay, the question here is "BECAUSE" the Statless session bean is created at the will of the Container and not the client, how can it have access to EJBObject.

The OID both HFEJB page : 227 and in spec Page : 92 suggest that the new EJBObject is created when the "client" calls create method of Home interface. So by this diagram one can come to a conclusion that during ejbCreate() of stateless session bean we cannot get access to EJBObject, because it will be created only when the client calls create() method.

But from Kathy's response (thanks to Adri Smith for pointing me to it) it appears that EJBObject can be created anytime and the bean's context will get reference to it during ejbCreate().

Quote from Roger Chung-Wee

that the container will pull a bean instance from the pool and associate the instance with its SessionContext and EJBObject by invoking the bean's setSessionContext() and ejbCreate() methods.


Roger, according to both life-cycle diagram and OID, setSessionContext() and ejbCreate() are called before the bean comes to the pool.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
The OID both HFEJB page : 227 and in spec Page : 92 suggest that the new EJBObject is created when the "client" calls create method of Home interface. So by this diagram one can come to a conclusion that during ejbCreate() of stateless session bean we cannot get access to EJBObject, because it will be created only when the client calls create() method.

You are understandably getting confused about this. When the spec's OID says that the creation of an EJBObject is triggered by the invocation of the create() method of the home interface, this is not to be taken literally. It just means that this is how you should think about it, ie the EJBObject is created and its stub is returned to the client. But remember, as the EJBObject is accessible from the instance's ejbCreate() method, which will certainly be invoked on server startup if that bean type is pooled, then the obvious deduction is that the EJBObject, or something which can return the EJBObject's reference, must therefore available in this scenario before the client invokes create().

The best way I can think of describing this is to say that there are two viewpoints.

From the client perspective, the EJBObject is created when the home interface create() method is invoked.

From the bean perspective, the EJBObject is created after setSessionContext() is invoked but before ejbCreate() is invoked.
Ana Nava
Greenhorn

Joined: May 25, 2004
Posts: 24
Roger, thanks for explaining again.

I am NOT confused. I just restated the initial confusion, and the reason for that confusion to Vijay. That is the reason I stated that "One can come to a conclusion". From Kathy's response, I clearly understood that creation of EJBObject is not as sequential as depicted in OID.

Also for sure setSessionContext and ejbCreate are called before the bean comes to the pool. No CONFUSION on that

Thanks again .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access to EJBObject from within ebjCreate of a stateless SessionBean