I have a problem with persisting my collected transaction updates and revision them when a transaction completes. I implemented a javax.transaction.Synchonization which, of course, has an beforeCompletion() and afterCompletion() method. In the beforeCompletion() I do some cleanup that should take place in the current transaction (Tx1). This works fine. The problem is that I also want to commit my updates that I created during a transaction for versioning. It should be isolated from Tx1 and run in another Transaction Tx2, so that even in the case of an update failure, Tx1 completes correctly. The afterCompletion() method is bothering me, because I can't start a new transaction in it, no matter what I try. Things that I tried and that failed: - Annotating afterCompletion() with @Transactional and @TransactionAttribute(REQUIRES_NEW) and let CMT do the rest - Moving the Update-commit-functionality to another Bean method, which I annotated with @Asynchronous - @Asynchronous + @Transactional and @TransactionAttribute(REQUIRES_NEW) with CMT - @Asynchronous + @Transactional and @TransactionAttribute(REQUIRES_NEW) with BMT, explicitly called UserTransaction.begin() and commit() - @Asynchronous + global injected EntityManager, trying to call em.getTransaction().begin() and commit() - @Asynchronoug + newly created EntityManager with EntityManagerFactory, trying to call em.getTransaction().begin() and commit() - Raised an asynchronous event from inside of the afterCompletion() method and annotated my update-commit-method with an @Observer - Raised an asynchronous event from inside of the afterCompletion() method and annotated my update-commit-method with an @Observer + annotated method with @Transactional and @TransactionalAttribute(REQUIRES_NEW)
Everything failes. Most of them with an "transaction is not active". Some of them (Container-managed-transactions) don't throw an exception, but they also don't write something to the database. When I explicitly call em.flush, I fail with similar exceptions.