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

The Problem

Developers need to control transaction boundaries (when they start and end), but writing transaction code in business logic is tedious and error-prone. There are two approaches: let the container handle it (declarative) or write it manually (programmatic).

Core Idea

Declarative (CMT): Container automatically starts/commits transactions based on XML configuration — no transaction code in bean. Programmatic (BMT): Bean explicitly calls begin() and commit()/abort() in Java code. Session beans and MDBs can use either; entity beans MUST use CMT.

How It Works

Declarative (CMT)

  1. Set <transaction-type>Container</transaction-type> in ejb-jar.xml
  2. Container intercepts method call → calls begin() on transaction service
  3. Delegates to bean for business logic
  4. Bean returns (or signals abort via exception/setRollbackOnly())
  5. Container calls commit() or abort() on transaction service
  6. Bean and client are unaware of transaction mechanics

Programmatic (BMT)

  1. Set <transaction-type>Bean</transaction-type> in ejb-jar.xml
  2. Bean manually calls begin() at start of transaction
  3. Bean performs business operations
  4. Bean calls commit() or abort() at end
  5. Full control over transaction boundaries — can have mini-transactions within a method

Visual Explanation

G cluster_cmt Declarative (CMT) cluster_bmt Programmatic (BMT) CBean Bean (no tx code) CContainer Container begin → delegate → commit CBean->CContainer method call BBean Bean begin() → biz → commit()

Key Properties

FeatureDeclarative (CMT)Programmatic (BMT)
Transaction codeNone in beanWritten in bean
ControlContainer-managedDeveloper-controlled
Mini-transactionsNot possiblePossible within a method
Bean types allowedAll (entity MUST use this)Session + MDB only
TuningVia XML (no source access)Via code changes

Connections

Edge Cases & Gotchas

  • Entity beans (BMP/CMP) CANNOT use BMT — it’s illegal (container calls ejbLoad/ejbStore, not bean)
  • BMT requires careful handling: forgetting commit() leaves transaction open
  • CMT transactions apply to entire method (can’t have mini-transactions)
  • Client-initiated transactions are separate from BMT/CMT (client controls, bean still uses BMT/CMT internally)