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 ]