Search in sources :

Example 1 with Synchronization

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);
    }
}
Also used : Transaction(jakarta.transaction.Transaction) SQLException(java.sql.SQLException) Synchronization(jakarta.transaction.Synchronization) RollbackException(jakarta.transaction.RollbackException) SQLException(java.sql.SQLException) RollbackException(jakarta.transaction.RollbackException) SystemException(jakarta.transaction.SystemException)

Aggregations

RollbackException (jakarta.transaction.RollbackException)1 Synchronization (jakarta.transaction.Synchronization)1 SystemException (jakarta.transaction.SystemException)1 Transaction (jakarta.transaction.Transaction)1 SQLException (java.sql.SQLException)1