Both BMP and CMP entity beans have lifecycle callbacks (ejbLoad, ejbStore) that are called by the container, not the bean itself. If a developer tries to use programmatic transactions (begin/commit in code), they can’t control when ejbLoad/ejbStore are called — leading to uncommitted transactions and data inconsistency.
Entity Beans (BMP and CMP) MUST use Container-Managed (Declarative) Transactions only. Programmatic (Bean-Managed) Transactions are ILLEGAL for entity beans. This is the “Golden Rule” of EJB transactions.
- When a transaction calls an entity bean, container calls
ejbLoad()first (loads DB data, acquires locks) - Business methods execute within the transaction
- On commit, container calls
ejbStore()(writes to DB, releases locks) - Transaction spans:
ejbLoad()→ business methods →ejbStore() - If entity beans used BMT: developer would call
begin()inejbLoad(), but container controls whenejbLoad()is called — so the transaction may never properly complete - Entity beans load/store data per transaction, not per method call — the container optimizes when to actually hit the database
- Mandatory CMT: Entity beans (BMP and CMP) MUST use container-managed transactions
- BMT is illegal: Programmatic transactions cannot be used with entity beans
- Transaction spans ejbLoad → business methods → ejbStore (not per-method)
- Entity beans load/store per transaction, not per method call (performance implication)
- Session beans and MDBs CAN use BMT or CMT (entity beans cannot)
- Built from: Transaction Demarcation — entity beans restricted to CMT only
- Built from: Entity Bean — rule applies to all entity beans (BMP and CMP)
- Built from: ejbLoad() — called by container within transaction
- Built from: ejbStore() — called by container within transaction
- Contrasts with: Session Bean — session beans can use BMT or CMT
- Contrasts with: MDB — MDBs can use BMT or CMT
- Related: CMT vs BMT — why CMT is mandatory for entities
- Performance problem: if each method call is a separate transaction, entity bean does DB read/write per get/set
- Solution: make transactions span multiple method calls (using transaction attributes)
- BMP developers often mistakenly try BMT — it’s explicitly illegal in EJB spec
- Entity beans don’t control when ejbLoad/ejbStore are called — container does