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).