O'Reilly logo

Head First EJB by Bert Bates, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required