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

The Problem

Business processes vary—some span multiple requests (like shopping carts), others are single-request (like credit card verification). One bean type can’t efficiently handle both. What are the two subtypes and when to use each?

Core Idea

Session beans have two subtypes based on how they handle conversational state:

  • Stateful Session Beans: Maintain client-specific state across multiple method calls. Dedicated per client. Used for multi-step processes (shopping cart, wizard forms).
  • Stateless Session Beans: Do not maintain conversational state. Each method call is independent. Pooled and shared across clients. Used for single-request tasks (calculations, verification).

How It Works

AspectStatefulStateless
StateRemembers across callsForgets after each call
ClientDedicated (one bean per client)Shared (pool, any client)
PoolingNo pooling (dedicated)Highly pooled
ExamplesShopping cart, bank tellerCredit check, video compression
ScalabilityLower (RAM per client)Higher (shared pool)

Visual Explanation

SessionSubtypes cluster_stateful Stateful Session Beans cluster_stateless Stateless Session Beans (Pool) SF1 Bean A (Client 1's cart: Item1, Item2) SF2 Bean B (Client 2's cart: Item3) SF3 Bean C (Client 3's cart: Item4, Item5) SL1 Bean Pool (No memory of past calls) C1 Client 1 SL1->C1 Method call C2 Client 2 SL1->C2 Method call C3 Client 3 SL1->C3 Method call

Key Properties

  • Conversation: Stateful = multi-method conversation; Stateless = single method
  • Stateful uses ejbActivate()/ejbPassivate():-Stateful beans can be passivated to disk
  • Stateless has no activation/passivation: No state to save
  • Declared in deployment descriptor: <session-type>Stateful</session-type> or Stateless

Connections

Edge Cases & Gotchas

  • Stateless can have instance variables: Just not client-specific state (e.g., a shared DB connection factory is fine)
  • Switching types: Change <session-type> in XML—no code changes needed (declarative)
  • Stateless for Web Services: Since EJB 2.1, stateless beans can expose Web Service endpoints