Search in sources :

Example 26 with TransactionCallbackWithoutResult

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

the class JmsTransactionManagerTests method testParticipatingTransactionWithCommit.

@Test
public void testParticipatingTransactionWithCommit() throws JMSException {
    ConnectionFactory cf = mock(ConnectionFactory.class);
    Connection con = mock(Connection.class);
    final Session session = mock(Session.class);
    given(cf.createConnection()).willReturn(con);
    given(con.createSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(session);
    JmsTransactionManager tm = new JmsTransactionManager(cf);
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    final JmsTemplate jt = new JmsTemplate(cf);
    jt.execute(new SessionCallback<Void>() {

        @Override
        public Void doInJms(Session sess) {
            assertTrue(sess == session);
            return null;
        }
    });
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            jt.execute(new SessionCallback<Void>() {

                @Override
                public Void doInJms(Session sess) {
                    assertTrue(sess == session);
                    return null;
                }
            });
        }
    });
    tm.commit(ts);
    verify(session).commit();
    verify(session).close();
    verify(con).close();
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(javax.jms.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SessionCallback(org.springframework.jms.core.SessionCallback) ConnectionFactory(javax.jms.ConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Session(javax.jms.Session) Test(org.junit.Test)

Example 27 with TransactionCallbackWithoutResult

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

the class JpaTransactionManagerTests method testInvalidIsolation.

@Test
public void testInvalidIsolation() {
    tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
    given(manager.isOpen()).willReturn(true);
    try {
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
            }
        });
        fail("Should have thrown InvalidIsolationLevelException");
    } catch (InvalidIsolationLevelException ex) {
    // expected
    }
    verify(manager).close();
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) InvalidIsolationLevelException(org.springframework.transaction.InvalidIsolationLevelException) Test(org.junit.Test)

Example 28 with TransactionCallbackWithoutResult

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithIllegalStateExceptionOnRollbackOnly.

@Test
public void jtaTransactionManagerWithIllegalStateExceptionOnRollbackOnly() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    willThrow(new IllegalStateException("no existing transaction")).given(ut).setRollbackOnly();
    try {
        JtaTransactionManager ptm = newJtaTransactionManager(ut);
        TransactionTemplate tt = new TransactionTemplate(ptm);
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                status.setRollbackOnly();
            }
        });
        fail("Should have thrown TransactionSystemException");
    } catch (TransactionSystemException 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)

Example 29 with TransactionCallbackWithoutResult

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

the class JtaTransactionManagerTests method doTestJtaTransactionManagerWithNoExceptionOnGlobalRollbackOnly.

private void doTestJtaTransactionManagerWithNoExceptionOnGlobalRollbackOnly(boolean failEarly) throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_MARKED_ROLLBACK, Status.STATUS_MARKED_ROLLBACK, Status.STATUS_MARKED_ROLLBACK);
    JtaTransactionManager tm = newJtaTransactionManager(ut);
    if (failEarly) {
        tm.setFailEarlyOnGlobalRollbackOnly(true);
    }
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    boolean outerTransactionBoundaryReached = false;
    try {
        assertTrue("Is new transaction", ts.isNewTransaction());
        TransactionTemplate tt = new TransactionTemplate(tm);
        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_ROLLED_BACK);
                    }
                });
            }
        });
        outerTransactionBoundaryReached = true;
        tm.commit(ts);
        fail("Should have thrown UnexpectedRollbackException");
    } catch (UnexpectedRollbackException ex) {
        // expected
        if (!outerTransactionBoundaryReached) {
            tm.rollback(ts);
        }
        if (failEarly) {
            assertFalse(outerTransactionBoundaryReached);
        } else {
            assertTrue(outerTransactionBoundaryReached);
        }
    }
    verify(ut).begin();
    if (failEarly) {
        verify(ut).rollback();
    } else {
        verify(ut).commit();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter)

Example 30 with TransactionCallbackWithoutResult

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndExisting.

@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExisting() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    Transaction tx = mock(Transaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    given(tm.suspend()).willReturn(tx);
    JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    verify(ut).begin();
    verify(ut).commit();
    verify(tm).resume(tx);
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) Transaction(javax.transaction.Transaction) MockJtaTransaction(org.springframework.tests.transaction.MockJtaTransaction) UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Aggregations

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