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

The Problem

When a stateful session bean is idle (no client activity for a while), the container may want to free up memory by serializing the bean to secondary storage. Before serialization, the bean needs to release any non-serializable resources (like open database connections, network sockets).

Core Idea

ejbPassivate() is a callback method that the EJB container calls on a stateful session bean BEFORE it is passivated (serialized to storage). It allows the bean to release resources that cannot be serialized.

How It Works

  1. Container decides — bean is idle, container chooses to passivate it
  2. ejbPassivate() called — BEFORE serialization, container calls this method
  3. Bean releases resources — close DB connections, JMS connections, etc.
  4. Serialization — container serializes bean to secondary storage
  5. Memory freed — bean instance returned to pool or garbage collected

When the client needs the bean again, ejbActivate() will be called to restore resources.

Visual Explanation

G Ready State Ready State Container: idle timeout Container: idle timeout Ready State->Container: idle timeout ejbPassivate() called ejbPassivate() called Container: idle timeout->ejbPassivate() called ejbPassivate() ejbPassivate() Release resources Release resources ejbPassivate()->Release resources Serialize to storage Serialize to storage Release resources->Serialize to storage Passivated State Passivated State Serialize to storage->Passivated State

Key Properties

  • Callback method — container calls it, not the client
  • Release non-serializable resources — main purpose
  • Stateful SB only — stateless beans don’t have passivation
  • No parameters — container doesn’t pass any arguments

Connections

Edge Cases & Gotchas

  • Only for stateful session beans (NOT stateless, NOT entity beans)
  • Don’t do business logic here — just cleanup
  • If this method throws an exception, passivation fails and bean stays in memory