0

For my Java EE (7) project I want to use the Axon framework. One of the parameters of the Axon ReplayingCluster is a TransactionManager, but Axon only supports NoTransactionManager and SpringTransactionManager.

But if i'm not mistaken with JTA, transactions are already managed by the application server in the EntityManager (which I have stored in the JpaEventStore).

My question: when I use ReplayingCluster with NoTransactionManager, does this mean JTA will still provide transactions functionality?

Albert Bos
  • 2,012
  • 1
  • 15
  • 26

1 Answers1

3

Event replays may involve thousands if not millions of events. Therefore it is often not feasible to manage a single replay in a single transaction (assuming your event listeners make changes that require transactions at all).

Axon uses a TransactionManager during replays to commit changes each time a batch of events is replayed. The size of this batch is configurable using the commitThreshold parameter.

Now, I don't have experience using JTA but my understanding is that a transaction is automatically created when a bean method is invoked and committed when that method returns. Meaning that when you trigger a replay it will do so in a single transaction.

My advice therefore is to supply your own implementation to the ReplayingCluster. On a EJB server this implementation may look something like this:

class JtaTransactionManager implements TransactionManager<UserTransaction> {

    @Resource
    private SessionContext ctx;

    @Override
    public UserTransaction startTransaction() {
        UserTransaction utx = ctx.getUserTransaction();
        utx.begin();
        return utx;
    }

    @Override
    public void commitTransaction(UserTransaction utx) {
        utx.commit();
    }

    @Override
    public void rollbackTransaction(UserTransaction utx) {
        utx.rollback();
    }

}

You can then annotate the bean that initiates the replay with @TransactionManagement(BEAN) to notify the application that you will manage these transactions yourself (this will not affect transaction management elsewhere).