use of jakarta.transaction.Synchronization in project tomcat by apache.
the class TransactionContext method addTransactionContextListener.
/**
* Adds a listener for transaction completion events.
*
* @param listener
* the listener to add
* @throws SQLException
* if a problem occurs adding the listener to the transaction
*/
public void addTransactionContextListener(final TransactionContextListener listener) throws SQLException {
try {
if (!isActive()) {
final Transaction transaction = this.transactionRef.get();
listener.afterCompletion(TransactionContext.this, transaction != null && transaction.getStatus() == Status.STATUS_COMMITTED);
return;
}
final Synchronization s = new Synchronization() {
@Override
public void afterCompletion(final int status) {
listener.afterCompletion(TransactionContext.this, status == Status.STATUS_COMMITTED);
}
@Override
public void beforeCompletion() {
// empty
}
};
if (transactionSynchronizationRegistry != null) {
transactionSynchronizationRegistry.registerInterposedSynchronization(s);
} else {
getTransaction().registerSynchronization(s);
}
} catch (final RollbackException e) {
// JTA spec doesn't let us register with a transaction marked rollback only
// just ignore this and the tx state will be cleared another way.
} catch (final Exception e) {
throw new SQLException("Unable to register transaction context listener", e);
}
}
Aggregations