Search in sources :

Example 1 with RollbackException

use of jakarta.transaction.RollbackException 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)

Example 2 with RollbackException

use of jakarta.transaction.RollbackException in project spring-framework by spring-projects.

the class JtaTransactionManagerTests method jtaTransactionManagerWithRollbackExceptionOnCommit.

@Test
public void jtaTransactionManagerWithRollbackExceptionOnCommit() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    willThrow(new RollbackException("unexpected rollback")).given(ut).commit();
    assertThatExceptionOfType(UnexpectedRollbackException.class).isThrownBy(() -> {
        JtaTransactionManager ptm = newJtaTransactionManager(ut);
        TransactionTemplate tt = new TransactionTemplate(ptm);
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                // something transactional
                TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {

                    @Override
                    public void afterCompletion(int status) {
                        assertThat(status == TransactionSynchronization.STATUS_ROLLED_BACK).as("Correct completion status").isTrue();
                    }
                });
            }
        });
    });
    verify(ut).begin();
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) HeuristicRollbackException(jakarta.transaction.HeuristicRollbackException) RollbackException(jakarta.transaction.RollbackException) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 3 with RollbackException

use of jakarta.transaction.RollbackException in project spring-framework by spring-projects.

the class JtaTransactionManager method doCommit.

@Override
protected void doCommit(DefaultTransactionStatus status) {
    JtaTransactionObject txObject = (JtaTransactionObject) status.getTransaction();
    try {
        int jtaStatus = txObject.getUserTransaction().getStatus();
        if (jtaStatus == Status.STATUS_NO_TRANSACTION) {
            // In any case, the transaction is already fully cleaned up.
            throw new UnexpectedRollbackException("JTA transaction already completed - probably rolled back");
        }
        if (jtaStatus == Status.STATUS_ROLLEDBACK) {
            // IllegalStateException expected on JBoss; call still necessary.
            try {
                txObject.getUserTransaction().rollback();
            } catch (IllegalStateException ex) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Rollback failure with transaction already marked as rolled back: " + ex);
                }
            }
            throw new UnexpectedRollbackException("JTA transaction already rolled back (probably due to a timeout)");
        }
        txObject.getUserTransaction().commit();
    } catch (RollbackException ex) {
        throw new UnexpectedRollbackException("JTA transaction unexpectedly rolled back (maybe due to a timeout)", ex);
    } catch (HeuristicMixedException ex) {
        throw new HeuristicCompletionException(HeuristicCompletionException.STATE_MIXED, ex);
    } catch (HeuristicRollbackException ex) {
        throw new HeuristicCompletionException(HeuristicCompletionException.STATE_ROLLED_BACK, ex);
    } catch (IllegalStateException ex) {
        throw new TransactionSystemException("Unexpected internal transaction state", ex);
    } catch (SystemException ex) {
        throw new TransactionSystemException("JTA failure on commit", ex);
    }
}
Also used : HeuristicRollbackException(jakarta.transaction.HeuristicRollbackException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) SystemException(jakarta.transaction.SystemException) HeuristicMixedException(jakarta.transaction.HeuristicMixedException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) HeuristicRollbackException(jakarta.transaction.HeuristicRollbackException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) RollbackException(jakarta.transaction.RollbackException) HeuristicCompletionException(org.springframework.transaction.HeuristicCompletionException)

Example 4 with RollbackException

use of jakarta.transaction.RollbackException in project tomcat by apache.

the class TransactionContext method setSharedConnection.

/**
 * Sets the shared connection for this transaction. The shared connection is enlisted in the transaction.
 *
 * @param sharedConnection
 *            the shared connection
 * @throws SQLException
 *             if a shared connection is already set, if XAResource for the connection could not be found in the
 *             transaction registry, or if there was a problem enlisting the connection in the transaction
 */
public void setSharedConnection(final Connection sharedConnection) throws SQLException {
    if (this.sharedConnection != null) {
        throw new IllegalStateException("A shared connection is already set");
    }
    // This is the first use of the connection in this transaction, so we must
    // enlist it in the transaction
    final Transaction transaction = getTransaction();
    try {
        final XAResource xaResource = transactionRegistry.getXAResource(sharedConnection);
        if (!transaction.enlistResource(xaResource)) {
            throw new SQLException("Unable to enlist connection in transaction: enlistResource returns 'false'.");
        }
    } catch (final IllegalStateException e) {
        // This can happen if the transaction is already timed out
        throw new SQLException("Unable to enlist connection in the transaction", e);
    } catch (final RollbackException e) {
    // transaction was rolled back... proceed as if there never was a transaction
    } catch (final SystemException e) {
        throw new SQLException("Unable to enlist connection the transaction", e);
    }
    this.sharedConnection = sharedConnection;
}
Also used : XAResource(javax.transaction.xa.XAResource) Transaction(jakarta.transaction.Transaction) SystemException(jakarta.transaction.SystemException) SQLException(java.sql.SQLException) RollbackException(jakarta.transaction.RollbackException)

Example 5 with RollbackException

use of jakarta.transaction.RollbackException in project spring-framework by spring-projects.

the class JtaTransactionManager method registerAfterCompletionWithExistingTransaction.

@Override
protected void registerAfterCompletionWithExistingTransaction(Object transaction, List<TransactionSynchronization> synchronizations) {
    JtaTransactionObject txObject = (JtaTransactionObject) transaction;
    logger.debug("Registering after-completion synchronization with existing JTA transaction");
    try {
        doRegisterAfterCompletionWithJtaTransaction(txObject, synchronizations);
    } catch (SystemException ex) {
        throw new TransactionSystemException("JTA failure on registerSynchronization", ex);
    } catch (Exception ex) {
        // Note: JBoss throws plain RuntimeException with RollbackException as cause.
        if (ex instanceof RollbackException || ex.getCause() instanceof RollbackException) {
            logger.debug("Participating in existing JTA transaction that has been marked for rollback: " + "cannot register Spring after-completion callbacks with outer JTA transaction - " + "immediately performing Spring after-completion callbacks with outcome status 'rollback'. " + "Original exception: " + ex);
            invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_ROLLED_BACK);
        } else {
            logger.debug("Participating in existing JTA transaction, but unexpected internal transaction " + "state encountered: cannot register Spring after-completion callbacks with outer JTA " + "transaction - processing Spring after-completion callbacks with outcome status 'unknown'" + "Original exception: " + ex);
            invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_UNKNOWN);
        }
    }
}
Also used : TransactionSystemException(org.springframework.transaction.TransactionSystemException) SystemException(jakarta.transaction.SystemException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) HeuristicRollbackException(jakarta.transaction.HeuristicRollbackException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) RollbackException(jakarta.transaction.RollbackException) HeuristicRollbackException(jakarta.transaction.HeuristicRollbackException) NamingException(javax.naming.NamingException) IllegalTransactionStateException(org.springframework.transaction.IllegalTransactionStateException) TransactionSystemException(org.springframework.transaction.TransactionSystemException) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) InvalidTransactionException(jakarta.transaction.InvalidTransactionException) HeuristicCompletionException(org.springframework.transaction.HeuristicCompletionException) TransactionSuspensionNotSupportedException(org.springframework.transaction.TransactionSuspensionNotSupportedException) NotSupportedException(jakarta.transaction.NotSupportedException) IOException(java.io.IOException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) InvalidIsolationLevelException(org.springframework.transaction.InvalidIsolationLevelException) RollbackException(jakarta.transaction.RollbackException) HeuristicMixedException(jakarta.transaction.HeuristicMixedException) NestedTransactionNotSupportedException(org.springframework.transaction.NestedTransactionNotSupportedException) SystemException(jakarta.transaction.SystemException)

Aggregations

RollbackException (jakarta.transaction.RollbackException)5 SystemException (jakarta.transaction.SystemException)4 HeuristicRollbackException (jakarta.transaction.HeuristicRollbackException)3 HeuristicMixedException (jakarta.transaction.HeuristicMixedException)2 Transaction (jakarta.transaction.Transaction)2 SQLException (java.sql.SQLException)2 HeuristicCompletionException (org.springframework.transaction.HeuristicCompletionException)2 TransactionSystemException (org.springframework.transaction.TransactionSystemException)2 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)2 InvalidTransactionException (jakarta.transaction.InvalidTransactionException)1 NotSupportedException (jakarta.transaction.NotSupportedException)1 Synchronization (jakarta.transaction.Synchronization)1 UserTransaction (jakarta.transaction.UserTransaction)1 IOException (java.io.IOException)1 NamingException (javax.naming.NamingException)1 XAResource (javax.transaction.xa.XAResource)1 Test (org.junit.jupiter.api.Test)1 CannotCreateTransactionException (org.springframework.transaction.CannotCreateTransactionException)1 IllegalTransactionStateException (org.springframework.transaction.IllegalTransactionStateException)1 InvalidIsolationLevelException (org.springframework.transaction.InvalidIsolationLevelException)1