Search in sources :

Example 91 with TransactionStatus

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

the class DataSourceTransactionManagerTests method testPropagationNotSupportedWithExistingTransaction.

@Test
public void testPropagationNotSupportedWithExistingTransaction() throws Exception {
    final TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            assertTrue("Is new transaction", status.isNewTransaction());
            assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
            assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
            tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
            tt.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
                    assertTrue("Synchronization active", TransactionSynchronizationManager.isSynchronizationActive());
                    assertTrue("Isn't new transaction", !status.isNewTransaction());
                    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
                    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
                    status.setRollbackOnly();
                }
            });
            assertTrue("Is new transaction", status.isNewTransaction());
            assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
            assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
        }
    });
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    verify(con).commit();
    verify(con).close();
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 92 with TransactionStatus

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

the class DataSourceTransactionManagerTests method testTransactionWithPropagationNotSupported.

@Test
public void testTransactionWithPropagationNotSupported() throws Exception {
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
            assertTrue("Is not new transaction", !status.isNewTransaction());
        }
    });
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 93 with TransactionStatus

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

the class DataSourceTransactionManagerTests method testParticipatingTransactionWithIncompatibleIsolationLevel.

@Test
public void testParticipatingTransactionWithIncompatibleIsolationLevel() throws Exception {
    tm.setValidateExistingTransaction(true);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
    try {
        final TransactionTemplate tt = new TransactionTemplate(tm);
        final TransactionTemplate tt2 = new TransactionTemplate(tm);
        tt2.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                assertFalse("Is not rollback-only", status.isRollbackOnly());
                tt2.execute(new TransactionCallbackWithoutResult() {

                    @Override
                    protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                        status.setRollbackOnly();
                    }
                });
                assertTrue("Is rollback-only", status.isRollbackOnly());
            }
        });
        fail("Should have thrown IllegalTransactionStateException");
    } catch (IllegalTransactionStateException ex) {
    // expected
    }
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    verify(con).rollback();
    verify(con).close();
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) IllegalTransactionStateException(org.springframework.transaction.IllegalTransactionStateException) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 94 with TransactionStatus

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

the class DataSourceTransactionManagerTests method testTransactionWithPropagationSupports.

@Test
public void testTransactionWithPropagationSupports() throws Exception {
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
            assertTrue("Is not new transaction", !status.isNewTransaction());
            assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
            assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
        }
    });
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 95 with TransactionStatus

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

the class JmsTransactionManagerTests method testTransactionCommit.

@Test
public void testTransactionCommit() 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());
    JmsTemplate jt = new JmsTemplate(cf);
    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 : ConnectionFactory(javax.jms.ConnectionFactory) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Connection(javax.jms.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) JmsTemplate(org.springframework.jms.core.JmsTemplate) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

TransactionStatus (org.springframework.transaction.TransactionStatus)359 Test (org.junit.Test)186 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)154 TransactionCallback (org.springframework.transaction.support.TransactionCallback)91 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)72 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)32 List (java.util.List)28 ArrayList (java.util.ArrayList)27 SQLException (java.sql.SQLException)25 Connection (java.sql.Connection)21 Date (java.util.Date)18 PreparedStatement (java.sql.PreparedStatement)17 TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)15 OnmsNode (org.opennms.netmgt.model.OnmsNode)14 ManagerException (com.alibaba.otter.manager.biz.common.exceptions.ManagerException)13 RepeatConfigureException (com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException)13 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)12 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)12 EntityManager (javax.persistence.EntityManager)11 DataSource (javax.sql.DataSource)11