Hi, I am having confusion in ejbCreate() method call for a Stateless bean. Container will create a pool of Stateless Session Beans(the setSessionContext() mtd call is decided by the Container). When client calls the Home create() mtd, the Container returns the EJBObject but at this time, the bean is still in the pool, there is no link between the EJBObject and Bean. The bean is associate to EJBObject only when the client invokes any business mtd on the EJBObject. So, when will be ejbCreate() mtd in the Bean is executed. Is it when the Container creates the bean or else when the client invokes the Home.create().
Entity Beans also pooled by the Continer. So, for this ejbCreate() is called when the client invokes Home.create() method.
For session bean the ejbCreate() mtd is called only once rite. So, for stateless bean it is called before the instance go to pool. Then for statefull session bean is it every time bcoz the bean is specify to the client.
The following is my understanding of the session bean lifecycle. Hope this helps you.
For a stateful session bean instance:
1. A client invokes create on the home. The container invokes newInstance on the session bean class to create a new session bean instance. Next, the container calls setSessionContext and ejbCreate and returns the session EJBObject reference to the client.
2. Now the client can invoke business methods on the instance. Now if the bean is inactive for some time, container will call ejbPassivate. Again if client invokes business method, ejbActivate will called.
3. The instance remains in memory as long as the client is invoking business methods. If the client calls remove() on the bean or if timeout occurs, the instance will be removed, so will the associated session object.
Now if the client wants the bean again, he has to invoke create again. So for the lifetime of a session bean, ejbCreate will be called only once.
Talking about stateless session bean,
1. the life of a stateless session bean starts when the container invokes the newInstance method on the session bean class. Next the container calls setSessionContext followed by ejbCreate on the instance. The container can do the above any time, independent of a client invoking create. The instance will now be in the pool.
2. When the client invokes create, only an EJBObject will be created which will be later linked to a bean instance in the pool to service client requests.
3. Similarly, the container can invoke ejbRemove on a bean instance at any time independent of a remove call from the client.
So, in case of stateless bean also, ejbCreate is invoked only once in a bean's life cycle.