Search in sources :

Example 1 with TransactionSystemException

use of cn.taketoday.transaction.TransactionSystemException in project today-infrastructure by TAKETODAY.

the class AbstractTransactionAspectTests method doTestRollbackOnException.

/**
 * Check that the given exception thrown by the target can produce the
 * desired behavior with the appropriate transaction attribute.
 *
 * @param ex exception to be thrown by the target
 * @param shouldRollback whether this should cause a transaction rollback
 */
@SuppressWarnings("serial")
protected void doTestRollbackOnException(final Exception ex, final boolean shouldRollback, boolean rollbackException) throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute() {

        @Override
        public boolean rollbackOn(Throwable t) {
            assertThat(t == ex).isTrue();
            return shouldRollback;
        }
    };
    Method m = exceptionalMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);
    TransactionStatus status = mock(TransactionStatus.class);
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    // Gets additional call(s) from TransactionControl
    given(ptm.getTransaction(txatt)).willReturn(status);
    TransactionSystemException tex = new TransactionSystemException("system exception");
    if (rollbackException) {
        if (shouldRollback) {
            willThrow(tex).given(ptm).rollback(status);
        } else {
            willThrow(tex).given(ptm).commit(status);
        }
    }
    TestBean tb = new TestBean();
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);
    try {
        itb.exceptional(ex);
        fail("Should have thrown exception");
    } catch (Throwable t) {
        if (rollbackException) {
            assertThat(t).as("Caught wrong exception").isEqualTo(tex);
        } else {
            assertThat(t).as("Caught wrong exception").isEqualTo(ex);
        }
    }
    if (!rollbackException) {
        if (shouldRollback) {
            verify(ptm).rollback(status);
        } else {
            verify(ptm).commit(status);
        }
    }
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TransactionStatus(cn.taketoday.transaction.TransactionStatus) Method(java.lang.reflect.Method) TransactionSystemException(cn.taketoday.transaction.TransactionSystemException) PlatformTransactionManager(cn.taketoday.transaction.PlatformTransactionManager)

Example 2 with TransactionSystemException

use of cn.taketoday.transaction.TransactionSystemException in project today-framework by TAKETODAY.

the class AbstractTransactionAspectTests method doTestRollbackOnException.

/**
 * Check that the given exception thrown by the target can produce the
 * desired behavior with the appropriate transaction attribute.
 *
 * @param ex exception to be thrown by the target
 * @param shouldRollback whether this should cause a transaction rollback
 */
@SuppressWarnings("serial")
protected void doTestRollbackOnException(final Exception ex, final boolean shouldRollback, boolean rollbackException) throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute() {

        @Override
        public boolean rollbackOn(Throwable t) {
            assertThat(t == ex).isTrue();
            return shouldRollback;
        }
    };
    Method m = exceptionalMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);
    TransactionStatus status = mock(TransactionStatus.class);
    PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
    // Gets additional call(s) from TransactionControl
    given(ptm.getTransaction(txatt)).willReturn(status);
    TransactionSystemException tex = new TransactionSystemException("system exception");
    if (rollbackException) {
        if (shouldRollback) {
            willThrow(tex).given(ptm).rollback(status);
        } else {
            willThrow(tex).given(ptm).commit(status);
        }
    }
    TestBean tb = new TestBean();
    ITestBean itb = (ITestBean) advised(tb, ptm, tas);
    try {
        itb.exceptional(ex);
        fail("Should have thrown exception");
    } catch (Throwable t) {
        if (rollbackException) {
            assertThat(t).as("Caught wrong exception").isEqualTo(tex);
        } else {
            assertThat(t).as("Caught wrong exception").isEqualTo(ex);
        }
    }
    if (!rollbackException) {
        if (shouldRollback) {
            verify(ptm).rollback(status);
        } else {
            verify(ptm).commit(status);
        }
    }
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TransactionStatus(cn.taketoday.transaction.TransactionStatus) Method(java.lang.reflect.Method) TransactionSystemException(cn.taketoday.transaction.TransactionSystemException) PlatformTransactionManager(cn.taketoday.transaction.PlatformTransactionManager)

Example 3 with TransactionSystemException

use of cn.taketoday.transaction.TransactionSystemException in project today-framework by TAKETODAY.

the class JdbcTransactionObjectSupport method rollbackToSavepoint.

/**
 * This implementation rolls back to the given JDBC 3.0 Savepoint.
 *
 * @see java.sql.Connection#rollback(Savepoint)
 */
@Override
public void rollbackToSavepoint(Object savepoint) throws TransactionException {
    ConnectionHolder conHolder = getConnectionHolderForSavepoint();
    try {
        conHolder.getConnection().rollback((Savepoint) savepoint);
        conHolder.resetRollbackOnly();
    } catch (Throwable ex) {
        throw new TransactionSystemException("Could not roll back to JDBC savepoint", ex);
    }
}
Also used : TransactionSystemException(cn.taketoday.transaction.TransactionSystemException)

Example 4 with TransactionSystemException

use of cn.taketoday.transaction.TransactionSystemException in project today-framework by TAKETODAY.

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.toString());
                }
            }
            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(cn.taketoday.transaction.TransactionSystemException) SystemException(jakarta.transaction.SystemException) HeuristicMixedException(jakarta.transaction.HeuristicMixedException) UnexpectedRollbackException(cn.taketoday.transaction.UnexpectedRollbackException) TransactionSystemException(cn.taketoday.transaction.TransactionSystemException) HeuristicRollbackException(jakarta.transaction.HeuristicRollbackException) RollbackException(jakarta.transaction.RollbackException) UnexpectedRollbackException(cn.taketoday.transaction.UnexpectedRollbackException) HeuristicCompletionException(cn.taketoday.transaction.HeuristicCompletionException)

Example 5 with TransactionSystemException

use of cn.taketoday.transaction.TransactionSystemException in project today-framework by TAKETODAY.

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 Framework after-completion callbacks with outer JTA transaction - " + "immediately performing Framework after-completion callbacks with outcome status 'rollback'. " + "Original exception: {}", ex.toString());
            invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_ROLLED_BACK);
        } else {
            logger.debug("Participating in existing JTA transaction, but unexpected internal transaction " + "state encountered: cannot register Framework after-completion callbacks with outer JTA " + "transaction - processing Framework after-completion callbacks with outcome status 'unknown'" + "Original exception: {}", ex.toString());
            invokeAfterCompletion(synchronizations, TransactionSynchronization.STATUS_UNKNOWN);
        }
    }
}
Also used : TransactionSystemException(cn.taketoday.transaction.TransactionSystemException) SystemException(jakarta.transaction.SystemException) TransactionSystemException(cn.taketoday.transaction.TransactionSystemException) HeuristicRollbackException(jakarta.transaction.HeuristicRollbackException) RollbackException(jakarta.transaction.RollbackException) UnexpectedRollbackException(cn.taketoday.transaction.UnexpectedRollbackException) HeuristicRollbackException(jakarta.transaction.HeuristicRollbackException) NamingException(javax.naming.NamingException) HeuristicCompletionException(cn.taketoday.transaction.HeuristicCompletionException) CannotCreateTransactionException(cn.taketoday.transaction.CannotCreateTransactionException) IllegalTransactionStateException(cn.taketoday.transaction.IllegalTransactionStateException) InvalidTransactionException(jakarta.transaction.InvalidTransactionException) InvalidIsolationLevelException(cn.taketoday.transaction.InvalidIsolationLevelException) NestedTransactionNotSupportedException(cn.taketoday.transaction.NestedTransactionNotSupportedException) NotSupportedException(jakarta.transaction.NotSupportedException) IOException(java.io.IOException) TransactionSuspensionNotSupportedException(cn.taketoday.transaction.TransactionSuspensionNotSupportedException) RollbackException(jakarta.transaction.RollbackException) HeuristicMixedException(jakarta.transaction.HeuristicMixedException) TransactionSystemException(cn.taketoday.transaction.TransactionSystemException) UnexpectedRollbackException(cn.taketoday.transaction.UnexpectedRollbackException) SystemException(jakarta.transaction.SystemException)

Aggregations

TransactionSystemException (cn.taketoday.transaction.TransactionSystemException)14 UnexpectedRollbackException (cn.taketoday.transaction.UnexpectedRollbackException)6 Method (java.lang.reflect.Method)6 CannotCreateTransactionException (cn.taketoday.transaction.CannotCreateTransactionException)4 HeuristicCompletionException (cn.taketoday.transaction.HeuristicCompletionException)4 PlatformTransactionManager (cn.taketoday.transaction.PlatformTransactionManager)4 ReactiveTransaction (cn.taketoday.transaction.ReactiveTransaction)4 ReactiveTransactionManager (cn.taketoday.transaction.ReactiveTransactionManager)4 TransactionStatus (cn.taketoday.transaction.TransactionStatus)4 HeuristicMixedException (jakarta.transaction.HeuristicMixedException)4 HeuristicRollbackException (jakarta.transaction.HeuristicRollbackException)4 RollbackException (jakarta.transaction.RollbackException)4 SystemException (jakarta.transaction.SystemException)4 Test (org.junit.jupiter.api.Test)4 Mono (reactor.core.publisher.Mono)4 BeanFactory (cn.taketoday.beans.factory.BeanFactory)2 BeanFactoryAware (cn.taketoday.beans.factory.BeanFactoryAware)2 InitializingBean (cn.taketoday.beans.factory.InitializingBean)2 BeanFactoryAnnotationUtils (cn.taketoday.beans.factory.annotation.BeanFactoryAnnotationUtils)2 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)2