jQuery in Action, 2nd edition*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes ejbCreate in session bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "ejbCreate in session bean" Watch "ejbCreate in session bean" New topic
Author

ejbCreate in session bean

Reghu Ram Thanumalayan
Ranch Hand

Joined: Oct 21, 2003
Posts: 193
Hi Kathy,
In the chapter 4 �Being a Session Bean� under the heading �the bean things you can do from stateless bean methods � (Page 228 ) in Head First EJB, its given that we can get a reference to Home & EJB Objects in the ejbCreate & ejbRemove methods.
But for a stateless Bean, the bean is not linked with the EJB Object until the client calls a business method on the EJB Object. So how is it possible to get a reference to the EJB Object or the Home interface in the ejbCreate method which is called at a completely different/unrelated time from the time the client calls create on the home object ?
Similarly for the stateful session beans, when is the bean instance linked with the EJB Object. Is it after the setSessionContext but before the ejbCreate because then only it is possible to get reference to the EJB Object in ejbCreate which is not possible in setSessionContext !
I am not sure if this doubt sounds silly but please make me clear.
Reghu Ram


Cheers,<br />Reghu Ram T<br /> <br />SCJP 1.4 - 98 %, SCBCD 1.3 - 94 %, SCMAD 1.0 - 92 %
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi Reghu,
But for a stateless Bean, the bean is not linked with the EJB Object until the client calls a business method on the EJB Object. So how is it possible to get a reference to the EJB Object or the Home interface in the ejbCreate method which is called at a completely different/unrelated time from the time the client calls create on the home object ?

Let's start with the easiest part of your question : access to home (SessionContext.getEJBHome()). As the home of any bean (except MDBs which have no home) is *unique* and must *preexist* beans life (clients of stateful session beans use the home to create bean instances), it's quite logical that any bean which has a home may access its home whatever its state (in a pool, in ready-state, passivated, ...).
For the getEJBObject() method it's a bit more confusing.
1. It's clear that you may call it from ejbCreate() for a stateless bean (see table 3 on p. 90 of the specs).
2. It contradicts a bit what's written on p. 101 and p. 103 of HF EJB, because stateless beans seem to be linked to their EJBObject only during business methods.
Both views are probably right. As the specs *state* that getEJBObject() can be called from ejbCreate(), any compliant EJB container *must* ensure that such a call is allowed. Period. Now the way it's implemented is let free to the container developers. A possible implementation could make the EJBObject/bean instance link in a lazy fashion (put in other words, the fact that you call SessionContext.getEJBObject() from ejbCreate() could create such a link). We cannot know for sure IMO.
I that's true, the contradiction with HF EJB is less apparent.
Now you may take the issue from another side, asking yourself : "Should the use of a stateless bean's EJBObject from its ejbCreate() method bring some issue(s) ?" :
EJBObject.getEJBHome() : no problem, the home preexist anyway.
EJBObject.getHandle() : used only by remote clients --> N/A
EJBObject.getPrimaryKey() : reserved to entity beans --> N/A
EJBObject.isIdentical() : used only by remote clients --> N/A
EJBObject.remove() : a bean calling ctx.getEJBObject().remove() would be quite suicidal, right ? --> N/A
So I'd just remember what's written in the specs : you may call SessionContext.getEJBObject() from ejbCreate(), period. How it's implemented, we don't know. Is it useful ? Probably not (the home could be useful but you may get it from the context itself anyway).
Hope this helped.
Best,
Phil.
PS: It looks like you posted twice the same question. Could you delete the other thread ? Thanks.
[ December 09, 2003: Message edited by: Philippe Maquet ]
Reghu Ram Thanumalayan
Ranch Hand

Joined: Oct 21, 2003
Posts: 193
Hi Phil,
Thanks a lot for your detailed reply.
As the home of any bean (except MDBs which have no home) is *unique* and must *preexist* beans life (clients of stateful session beans use the home to create bean instances), it's quite logical that any bean which has a home may access its home whatever its state (in a pool, in ready-state, passivated, ...).

I agree that the home must preexist the bean's life but for stateless bean untill the linking between the EJB Object and the bean is created, how can the bean know who is the home ?
So i guess it goes this way. as per the spec, its possible to call the getEJBObject on the SessionContext from the ejbCreate method for a stateless session bean. How the container implements it , is of no importance to us.
And i am not able to delete the topic i inadverdently posted twice. I get an error message that only administrators and moderators can do that.
Thanks again Phil for making me clear,
Cheers,
Reghu Ram.
Mohana Murali
Greenhorn

Joined: Sep 26, 2002
Posts: 27
Simple Rule to remember:
Accessing EJBObject and EJBHome from:
ejbCreate()
ejbRemove() methods.
SessionBeans with BMT/CMT, stateful/stateless can access from ejbCreate() and ejbRemove()
Where as CMT entity bean can access from ejbPostCreate() (Can NOT from ejbCreate()) and ejbRemove().
No EJBObject and EJBHome for MDB.
Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
Still that does not answer why ejbCreate in stateless bean is allowed to get EJBObject. This seems nonsense to me. Or the diagram in the book is wrong. Or the specification itself is wrong. In other words this is legal call, furthermore it's legal by specification and still it can bring any results which are not covered by spec. Sorry I haven't read spec myself, I am judging by the book and your comments.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ejbCreate in session bean
 
Similar Threads
When is ejbCreate() called?
ejbCreate in session bean
Stateless Sessionbean ejbCreate() mtd
allowed operation in ejbCreate() of stateless session bean
HFE Page 228: sessionContext.getEJBObject() in ejbCreate() of stateless session bean.