Search in sources :

Example 31 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.

the class TransactionSupportTests method transactionTemplateWithRollbackException.

@SuppressWarnings("serial")
@Test
public void transactionTemplateWithRollbackException() {
    final TransactionSystemException tex = new TransactionSystemException("system exception");
    TestTransactionManager tm = new TestTransactionManager(false, true) {

        @Override
        protected void doRollback(DefaultTransactionStatus status) {
            super.doRollback(status);
            throw tex;
        }
    };
    TransactionTemplate template = new TransactionTemplate(tm);
    final RuntimeException ex = new RuntimeException("Some application exception");
    try {
        template.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                throw ex;
            }
        });
        fail("Should have propagated RuntimeException");
    } catch (RuntimeException caught) {
        // expected
        assertTrue("Correct exception", caught == tex);
        assertTrue("triggered begin", tm.begin);
        assertTrue("no commit", !tm.commit);
        assertTrue("triggered rollback", tm.rollback);
        assertTrue("no rollbackOnly", !tm.rollbackOnly);
    }
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) DefaultTransactionStatus(org.springframework.transaction.support.DefaultTransactionStatus) DefaultTransactionStatus(org.springframework.transaction.support.DefaultTransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 32 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.

the class TransactionSupportTests method transactionTemplateWithCallbackPreference.

@Test
public void transactionTemplateWithCallbackPreference() {
    MockCallbackPreferringTransactionManager ptm = new MockCallbackPreferringTransactionManager();
    TransactionTemplate template = new TransactionTemplate(ptm);
    template.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
        }
    });
    assertSame(template, ptm.getDefinition());
    assertFalse(ptm.getStatus().isRollbackOnly());
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) DefaultTransactionStatus(org.springframework.transaction.support.DefaultTransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 33 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.

the class TransactionSupportTests method transactionTemplateWithError.

@Test
public void transactionTemplateWithError() {
    TestTransactionManager tm = new TestTransactionManager(false, true);
    TransactionTemplate template = new TransactionTemplate(tm);
    try {
        template.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                throw new Error("Some application error");
            }
        });
        fail("Should have propagated Error");
    } catch (Error err) {
        // expected
        assertTrue("triggered begin", tm.begin);
        assertTrue("no commit", !tm.commit);
        assertTrue("triggered rollback", tm.rollback);
        assertTrue("no rollbackOnly", !tm.rollbackOnly);
    }
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) DefaultTransactionStatus(org.springframework.transaction.support.DefaultTransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 34 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.

the class JtaTransactionManagerTests method jtaTransactionManagerWithHeuristicRollbackExceptionOnCommit.

@Test
public void jtaTransactionManagerWithHeuristicRollbackExceptionOnCommit() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    willThrow(new HeuristicRollbackException("heuristic exception")).given(ut).commit();
    try {
        JtaTransactionManager ptm = newJtaTransactionManager(ut);
        TransactionTemplate tt = new TransactionTemplate(ptm);
        tt.execute(new TransactionCallbackWithoutResult() {

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

                    @Override
                    public void afterCompletion(int status) {
                        assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_UNKNOWN);
                    }
                });
            }
        });
        fail("Should have thrown HeuristicCompletionException");
    } catch (HeuristicCompletionException ex) {
        // expected
        assertTrue(ex.getOutcomeState() == HeuristicCompletionException.STATE_ROLLED_BACK);
    }
    verify(ut).begin();
}
Also used : UserTransaction(javax.transaction.UserTransaction) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Test(org.junit.Test)

Example 35 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project spring-framework by spring-projects.

the class JtaTransactionManagerTests method jtaTransactionManagerWithUnsupportedOperationExceptionOnNestedBegin.

@Test
public void jtaTransactionManagerWithUnsupportedOperationExceptionOnNestedBegin() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    willThrow(new UnsupportedOperationException("not supported")).given(ut).begin();
    try {
        JtaTransactionManager ptm = newJtaTransactionManager(ut);
        TransactionTemplate tt = new TransactionTemplate(ptm);
        tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            }
        });
        fail("Should have thrown NestedTransactionNotSupportedException");
    } catch (NestedTransactionNotSupportedException ex) {
    // expected
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Aggregations

TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)203 TransactionStatus (org.springframework.transaction.TransactionStatus)154 Test (org.junit.Test)120 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)104 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)50 UserTransaction (javax.transaction.UserTransaction)43 SQLException (java.sql.SQLException)19 Connection (java.sql.Connection)14 TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)14 ManagerException (com.alibaba.otter.manager.biz.common.exceptions.ManagerException)13 RepeatConfigureException (com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException)13 Date (java.util.Date)13 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)13 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)12 List (java.util.List)11 DataSource (javax.sql.DataSource)10 TransactionManager (javax.transaction.TransactionManager)9 InOrder (org.mockito.InOrder)9 ArrayList (java.util.ArrayList)8 SystemException (javax.transaction.SystemException)8