Enterprise Java Development@TOPIC@

Chapter 101. Container-Managed Transactions

101.1. Transaction Scope
101.1.1. Transaction Not Supported
101.1.2. Transaction Required
101.1.3. Transaction Supports
101.1.4. Transaction Requires New
101.1.5. Transaction Mandatory
101.1.6. Transaction Never
101.1.7. All Transaction Scopes
101.2. @Asynchronous Methods and Transactions
101.3. EJB Lifecycle Methods
101.4. Callback Methods
101.4.1. Message Driven Callback
101.4.2. Timer Callbacks @Asynchronous
101.5. EJB Control of Container-Managed Transactions
101.5.1. EJBContext
101.6. Exceptions
101.6.1. Checked/Application Exceptions
101.6.2. Checked/Application Exception Rollback
101.7. Persistence Context Propagation
101.8. Summary

  • EJB transactions cannot propagate across @Asynchronous methods

    • Use of REQUIRED in @Asynchronous method will act like REQUIRES_NEW



  • getUserTransaction()

    • Returned instance can use used to demarcate transactions

    • Restricted to EJBs using bean-managed transactions

  • getRollbackOnly()

    • Tests whether current transaction is going to be rolled back at the end

    • Restricted to EJBs using container-managed transactions

  • setRollbackOnly()

    • Allows EJBs to trigger the current transaction to be rolled back at the end

    • Restricted to EJBs using container-managed transactions


Resources created/modified within the transaction -- including resources operated on by called EJBs -- will be rolled back.


Even though the application did not throw an exception and gracefully returned a result -- the IT test shows the entity was not stored during the callback

No automatic rollback by default


Checked exceptions, but default, do not trigger rollbacks


An error in processing can be reported with an exception without rollback


IT test verifies entity thrown with the exception was persisted

Checked/Application exceptions can be configured to trigger a rollback


Annotation tells container to automatically rollback transaction if thrown


An error in processing can be reported with an exception without rollback


IT test verifies entity thrown with the exception was persisted


  • Persistence contexts can also get propagated

    • Stateless EJB can propagate its tx-scope persistence context to a called EJB

    • Stateful EJB can propagate its tx-scoped or extended persistence context to a called EJB

    • Stateless EJB can work with extended persistence context provided by upstream Stateful client

    • Stateful EJB cannot transform propagated tx-scope persistence context into an extended

  • EJB Facade can act as sharing point for common persistence context