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

The Problem

Transactions need clear boundaries: who starts (begin), who ends (commit/abort), and when. Without demarcation, there’s ambiguity about transaction ownership, leading to errors where transactions are never committed or are committed at the wrong time.

Core Idea

EJB supports three ways to demarcate (define boundaries of) transactions: Programmatic (bean writes begin/commit in code), Declarative (container automatically handles it via XML config), and Client-Initiated (client code controls transaction).

How It Works

1. Programmatic Transactions (Bean-Managed - BMT)

  • Bean explicitly calls begin(), commit(), abort() in Java code
  • Full control over transaction boundaries; can run mini-transactions within a method
  • Only allowed for: Session Beans and Message-Driven Beans
  • Illegal for Entity Beans (container calls ejbLoad/ejbStore, not the bean)

2. Declarative Transactions (Container-Managed - CMT)

  • No transaction code in bean; container auto-starts transaction on method entry, commits on exit
  • Configured in ejb-jar.xml: <transaction-type>Container</transaction-type>
  • Bean signals abort via exceptions or setRollbackOnly()
  • Required for all Entity Beans (BMP and CMP)

3. Client-Initiated Transactions

  • Client code (servlet, JSP, other EJB) begins and ends the transaction
  • Bean still uses programmatic or declarative internally
  • Downside: network failures cause more rollbacks in distributed systems
  • Use sparingly, especially for remote clients

Visual Explanation

G cluster_bmt Programmatic (BMT) cluster_cmt Declarative (CMT) cluster_client Client-Initiated BBean Bean Code begin() ...business... commit()/abort() CBean Bean Code (no tx code) Container Container begin() → delegate → commit()/abort() CBean->Container method call Client Client Code begin() ...call bean... commit()/abort()

Key Properties

  • BMT: Full control, mini-transactions possible, but more coding burden
  • CMT: Simpler code, container handles everything, tuning without source access
  • Client-Initiated: Client knows exact transaction outcome, but network issues cause rollbacks
  • Entity Beans MUST use CMT (BMT is illegal for entity beans)
  • Session Beans and MDBs can use BMT or CMT

Connections

Edge Cases & Gotchas

  • BMT illegal for Entity Beans because container (not bean) calls ejbLoad/ejbStore
  • Forgetting to call commit() in BMT causes transaction to never complete
  • Client-initiated transactions over WAN have high rollback rates due to network failures
  • BMT allows mini-transactions within a method; CMT/Client-Initiated apply to entire method