A session bean stops existing for one of three reasons:
the client calls remove()
the bean times out
the bean throws a system exception
But... there’s another question to ask: when the bean times out, was it active or passive? If the bean is active, the Container deals with it in the same way it deals with client remove() calls—the bean gets an ejbRemove() call and is then killed. But if the bean was passivated when it times out, the Container sends it straight to the does not exist state without calling ejbRemove().
Lifecycle overview: removing a stateful bean
Client calls remove() on an active (i.e. non-passivated) bean
Client calls remove on the component interface (or calls the remove() method in the home interface, that takes a Handle).
Container calls ejbRemove() on the bean.
Container “kills” the bean and EJB object (assume they’re now eligible for garbage collection). The client will get an exception if she tries to use the stub again.
Lifecycle overview: removing a stateful bean
Bean times out while active
Client doesn’t make any calls to the bean’s component interface for a long time (whatever the Container considers a “long” time).
Container decides to kill the bean, and calls ejbRemove() on the bean.
Container “kills” the bean and EJB object (assume they’re now eligible for garbage collection). The client will get an exception if she tries to use the stub again.
Lifecycle overview: removing a stateful bean
Bean times out while passivated
The client doesn’t call any methods on the bean’s component interface for a long time AFTER the bean has already been passivated.
Container decides to kill the bean, but does NOT call ejbRemove().
Container “kills” the bean and EJB object (assume they’re now eligible for garbage collection). The client will get an exception if she tries to use the stub again.
Lifecycle overview: removing a stateful bean
Bean throws a system exception
The bean throws a system (unchecked) exception while executing a method.
Container decides to kill the bean, but does NOT call ejbRemove().
Container “kills” the bean and EJB object (assume they’re now eligible for garbage collection). The client will get an exception if she tries to use the stub again.
Get Head First EJB now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.