Enterprise Java Development@TOPIC@

Chapter 103. Container-Managed Transactions

103.1. Transaction Scope
103.1.1. Transaction Not Supported
103.1.2. Transaction Required
103.1.3. Transaction Supports
103.1.4. Transaction Requires New
103.1.5. Transaction Mandatory
103.1.6. Transaction Never
103.1.7. All Transaction Scopes
103.2. @Asynchronous Methods and Transactions
103.3. EJB Lifecycle Methods
103.4. Callback Methods
103.4.1. Message Driven Callback
103.4.2. Timer Callbacks @Asynchronous
103.5. EJB Control of Container-Managed Transactions
103.5.1. EJBContext
103.6. Exceptions
103.6.1. Checked/Application Exceptions
103.6.2. Checked/Application Exception Rollback
103.7. Persistence Context Propagation
103.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