Bean Removal: when beans die

image with no caption
image with no caption

A session bean stops existing for one of three reasons:

  1. the client calls remove()

  2. the bean times out

  3. 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

  1. Client calls remove on the component interface (or calls the remove() method in the home interface, that takes a Handle).

    image with no caption
  2. Container calls ejbRemove() on the bean.

    image with no caption
  3. 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.

    image with no caption

Lifecycle overview: removing a stateful bean

Bean times out while active

  1. Client doesn’t make any calls to the bean’s component interface for a long time (whatever the Container considers a “long” time).

    image with no caption
  2. Container decides to kill the bean, and calls ejbRemove() on the bean.

    image with no caption
  3. 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.

    image with no caption

Lifecycle overview: removing a stateful bean

Bean times out while passivated

  1. The client doesn’t call any methods on the bean’s component interface for a long time AFTER the bean has already been passivated.

    image with no caption
  2. Container decides to kill the bean, but does NOT call ejbRemove().

    image with no caption
  3. 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.

    image with no caption

Lifecycle overview: removing a stateful bean

Bean throws a system exception

  1. The bean throws a system (unchecked) exception while executing a method.

    image with no caption
  2. Container decides to kill the bean, but does NOT call ejbRemove().

    image with no caption
  3. 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.

    image with no caption

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.