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).
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.
- Set
<transaction-type>Container</transaction-type>inejb-jar.xml - Container intercepts method call → calls
begin()on transaction service - Delegates to bean for business logic
- Bean returns (or signals abort via exception/
setRollbackOnly()) - Container calls
commit()orabort()on transaction service - Bean and client are unaware of transaction mechanics
- Set
<transaction-type>Bean</transaction-type>inejb-jar.xml - Bean manually calls
begin()at start of transaction - Bean performs business operations
- Bean calls
commit()orabort()at end - Full control over transaction boundaries — can have mini-transactions within a method
| Feature | Declarative (CMT) | Programmatic (BMT) |
|---|---|---|
| Transaction code | None in bean | Written in bean |
| Control | Container-managed | Developer-controlled |
| Mini-transactions | Not possible | Possible within a method |
| Bean types allowed | All (entity MUST use this) | Session + MDB only |
| Tuning | Via XML (no source access) | Via code changes |
- Built from: Transactions — CMT and BMT are demarcation styles
- Built from: Transaction Demarcation — the 3 ways to demarcate
- Builds into: Entity Bean Transaction Rules — entity beans MUST use CMT
- Related: MDB — MDBs can use BMT or CMT
- Related: Session Bean — session beans can use BMT or CMT
- Contrasts with: Client-Initiated — third demarcation style
- 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)