I have just looked at the EJB 2.0 spec and it says the following about the invocation of ejbCreate.
A stateless session bean instance’s life starts when the container invokes newInstance() on the session bean class to create a new instance. Next, the container calls setSession-Context() followed by ejbCreate() on the instance. The container can perform the instance creation at any time—there is no relationship to a client’s invocation of the create()
So basically the container can call ejbCreate when it wants to, this could either be just before it adds it to the pool or on every call to create on the home interface.
The best place to do your setup would be in setEntityContext as this is typically called once per pooled instance.
Hope this helps,
Thanks -- that's very helpful. I had been accomplishing *almost* the same thing by just setting an initialized flag after the first time through so that my initialization steps didn't need to occur twice. Your method definitely sounds better... :)
There should be no need to use an initialized flag. The spec says that ejbCreate will be called exactly one time per stateless session bean instace - when the container decides to create a new instance. In my opinion ejbCreate is better suited for setting up bean configuration as setSessionContext for stateless sessions, simply because yout have access to more of the methods of SessionContext and in all other ways this methods are equal. But this only applies to stateless sessions. When jboss calls ejbCreate more times on a bean instance it is an serrious error, but i dont believe that, someone would have noticed before, so possibly there is an error in your code.
Are'nt specs wonderful? They are open to interpration.
What Paul Austin (p_d_austin) quotes "The container
can perform the instance creation at any time?there
is no relationship to a client?s invocation of the
create() method.". Is quite correct. But what does
My experience with various EJB containers (JBoss
included) is that at any time, a container will
perform the steps as per spec, in that order when it
feels it needs to create a new stateless session
bean. They are all performed just before the bean
goes into the instance pool and are performed *once*.
There is *no* relationship to a create() invocation
on a bean home and the steps performed. The steps
are performed 1-2-3 for all purposes without deplay
Juraj Vasko (lothar) is also quite correct the
way the steps are performed per spec, ejbCreate()
is the appropriate point of bean initialisation as
you have access to more "goodies".
If the ejbCreate is being called more than once for a
*specfic* bean instance, then that most certainly is a
bug in the EJB container.
When you see ejbCreate() being called multiple times
are you _sure_ it is being called repeatedly for the
same bean instance, or might it be an artifact of
the way you are reporting/recording the ejbCreate()
Just some random thoughts...
Well, this problem seems to have gone away with the 2.4 final code release.
BTW, I had been just recording calls to ejbCreate() with a simple System.out.println() whenever ejbCreate() was called -- I had noticed it was called anytime a client called lookup. Could have been some weirdness with the particular build I was using.
Thanks for your insight into this problem!
yep it works with 2.4.3! finally!