Search in sources :

Example 11 with SystemException

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithSystemExceptionOnRollbackOnly.

@Test
public void jtaTransactionManagerWithSystemExceptionOnRollbackOnly() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    willThrow(new SystemException("system exception")).given(ut).setRollbackOnly();
    assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() -> {
        JtaTransactionManager ptm = newJtaTransactionManager(ut);
        TransactionTemplate tt = new TransactionTemplate(ptm);
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                status.setRollbackOnly();
                TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {

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

Example 12 with SystemException

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithSystemExceptionOnCommit.

@Test
public void jtaTransactionManagerWithSystemExceptionOnCommit() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    willThrow(new SystemException("system exception")).given(ut).commit();
    assertThatExceptionOfType(TransactionSystemException.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_UNKNOWN).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) SystemException(jakarta.transaction.SystemException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 13 with SystemException

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

SystemException (jakarta.transaction.SystemException)13 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)8 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)8 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)8 UserTransaction (jakarta.transaction.UserTransaction)7 Test (org.junit.jupiter.api.Test)7 Transaction (jakarta.transaction.Transaction)4 SQLException (java.sql.SQLException)4 RollbackException (jakarta.transaction.RollbackException)3 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)3 HeuristicMixedException (jakarta.transaction.HeuristicMixedException)2 HeuristicRollbackException (jakarta.transaction.HeuristicRollbackException)2 TransactionManager (jakarta.transaction.TransactionManager)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 IOException (java.io.IOException)1 Connection (java.sql.Connection)1