[hibernate-dev] JTA synchronizations on WildFly

Gunnar Morling gunnar at hibernate.org
Tue Mar 10 09:03:51 EDT 2015


I'm trying to perform a specific action upon transaction rollback.

Assuming this could be done using a custom
javax.transaction.Synchronization, I tried to register a synchronization as

    TransactionImplementor transaction = ...; // e.g. a CMTTransaction
    transaction.registerSynchronization( new MySync() );

And indeed beforeCompletion() is invoked as expected. But afterCompletion()
never is. I debugged this a bit on WildFly and observed the following:

* Hibernate ORM registers RegisteredSynchronization with JTA.
RegisteredSynchronization manages (indirectly, through
TransactionCoordinator, SynchronizationRegistry etc.) those
synchronizations added through o.h.t.Transaction.registerSynchronization()
* WildFly (specifically, TransactionUtil [1]) registers its own
for closing the entity manager/session

Now that second synchronization is called first, closing the session. Upon
SessionImpl#close(), the SynchronizationRegistry is cleared. Then when
afterComplection() is called on RegisteredSynchronization afterwards, any
previously registered delegate synchronizations are gone already and thus
do not get invoked.

I believe I found a workaround for my case by registering my
synchronization through JtaPlatform#registerSynchronization() (which
registers it with the actual JTA transaction).

My questions are:

* Is this behaviour expected?
* Is the work-around of doing the registration via JtaPlatform viable or
are there any drawbacks which I'm not aware of?




More information about the hibernate-dev mailing list