posted 16 years ago
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.
- Mini (preparing for SCBCD)