How it works in EJB

Transactions can propagate through method calls

When a bean is running code in a transaction, and calls a method on another bean, three different scenarios are possible:

  1. The called method runs in the caller’s transaction.

  2. The called method runs without a transaction.

  3. The called method runs within its own new transaction.

  1. The transaction started in the first method propagates to all other methods in the call stack. All called methods run within the same transaction. (In this book, we’ll abbreviate “transaction” to “tx”.)

    image with no caption

    So, what does it mean for multiple methods to run in the same transaction context? That depends on the bean type and what the beans do in their code. For example, imagine Bean A has a method with JDBC code that does an update to a database row. If any other method in the same transaction causes a rollback, Bean A’s update won’t commit, even if the database would have been more than happy to do it.

Get Head First EJB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.