• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

Developers often put critical cleanup code in ejbRemove()—closing database connections, deleting temporary shopping cart data, etc. But what if ejbRemove() is never called?

Core Idea

The EJB container MAY call ejbRemove() when it decides to destroy a bean instance. BUT it is NOT guaranteed. If the server crashes, or if a critical system exception occurs, the bean is destroyed without ejbRemove() being called. Never rely on ejbRemove() for critical cleanup.

How It Works

  • Normal case: Container calls ejbRemove() when client calls remove() or when bean times out
  • Crash case: Server crashes → ejbRemove() never called → resources leaked
  • Exception case: System exception occurs → container discards bean → ejbRemove() may not be called

Solution: Use external cleanup utilities (e.g., a periodic job that deletes abandoned shopping carts from the database).

Visual Explanation

EjbRemove Normal Normal Scenario Client calls remove() C1 Container calls ejbRemove() Normal->C1 Crash Crash Scenario Server crashes C2 ejbRemove() NEVER CALLED Crash->C2 Exception Exception Scenario System exception thrown C3 Container discards bean May not call ejbRemove() Exception->C3

Key Properties

  • Not a destructor: Unlike C++ destructors, ejbRemove() is not guaranteed
  • Container’s choice: EJB spec allows container to skip ejbRemove() in some cases
  • Periodic cleanup needed: For resources that MUST be cleaned (like temp DB records), use a scheduled job
  • Session beans: Stateless beans may be destroyed without ejbRemove() (pool management)

Connections

Edge Cases & Gotchas

  • Shopping cart example: If ejbRemove() isn’t called, abandoned carts stay in DB forever—need a cleanup job
  • Database connections: Don’t close them in ejbRemove()—use ejbPassivate() or let container manage pooling
  • Exam trick question: “Where should you put critical cleanup code?” → Answer: Nowhere in the bean—use external utilities