Search in sources :

Example 76 with TransactionStatus

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

the class DataSourceTransactionManagerTests method testTransactionWithExceptionOnCommit.

@Test
public void testTransactionWithExceptionOnCommit() throws Exception {
    willThrow(new SQLException("Cannot commit")).given(con).commit();
    TransactionTemplate tt = new TransactionTemplate(tm);
    try {
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            }
        });
        fail("Should have thrown TransactionSystemException");
    } catch (TransactionSystemException ex) {
    // expected
    }
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    verify(con).close();
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionSystemException(org.springframework.transaction.TransactionSystemException) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 77 with TransactionStatus

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

the class DataSourceTransactionManagerTests method testPropagationRequiresNewWithExistingTransactionAndUnrelatedFailingDataSource.

@Test
public void testPropagationRequiresNewWithExistingTransactionAndUnrelatedFailingDataSource() throws Exception {
    final DataSource ds2 = mock(DataSource.class);
    SQLException failure = new SQLException();
    given(ds2.getConnection()).willThrow(failure);
    final TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    DataSourceTransactionManager tm2 = new DataSourceTransactionManager(ds2);
    tm2.setTransactionSynchronization(DataSourceTransactionManager.SYNCHRONIZATION_NEVER);
    final TransactionTemplate tt2 = new TransactionTemplate(tm2);
    tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds2));
    assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
    try {
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                assertTrue("Is new transaction", status.isNewTransaction());
                assertTrue("Synchronization active", TransactionSynchronizationManager.isSynchronizationActive());
                assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
                assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
                tt2.execute(new TransactionCallbackWithoutResult() {

                    @Override
                    protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                        status.setRollbackOnly();
                    }
                });
            }
        });
        fail("Should have thrown CannotCreateTransactionException");
    } catch (CannotCreateTransactionException ex) {
        assertSame(failure, ex.getCause());
    }
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds2));
    verify(con).rollback();
    verify(con).close();
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 78 with TransactionStatus

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

the class DataSourceTransactionManagerTests method testTransactionWithEnforceReadOnly.

@Test
public void testTransactionWithEnforceReadOnly() throws Exception {
    tm.setEnforceReadOnly(true);
    given(con.getAutoCommit()).willReturn(true);
    Statement stmt = mock(Statement.class);
    given(con.createStatement()).willReturn(stmt);
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    tt.setReadOnly(true);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
            assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
        // something transactional
        }
    });
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    InOrder ordered = inOrder(con, stmt);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(stmt).executeUpdate("SET TRANSACTION READ ONLY");
    ordered.verify(stmt).close();
    ordered.verify(con).commit();
    ordered.verify(con).setAutoCommit(true);
    ordered.verify(con).close();
}
Also used : InOrder(org.mockito.InOrder) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 79 with TransactionStatus

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

the class DataSourceTransactionManagerTests method testTransactionAwareDataSourceProxyWithSuspensionAndReobtaining.

@Test
public void testTransactionAwareDataSourceProxyWithSuspensionAndReobtaining() throws Exception {
    given(con.getAutoCommit()).willReturn(true);
    final TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            assertEquals(con, DataSourceUtils.getConnection(ds));
            final TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
            dsProxy.setReobtainTransactionalConnections(true);
            try {
                assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
                // should be ignored
                dsProxy.getConnection().close();
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
            tt.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    // something transactional
                    assertEquals(con, DataSourceUtils.getConnection(ds));
                    try {
                        assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
                        // should be ignored
                        dsProxy.getConnection().close();
                    } catch (SQLException ex) {
                        throw new UncategorizedSQLException("", "", ex);
                    }
                }
            });
            try {
                assertEquals(con, ((ConnectionProxy) dsProxy.getConnection()).getTargetConnection());
                // should be ignored
                dsProxy.getConnection().close();
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
        }
    });
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).commit();
    ordered.verify(con).setAutoCommit(true);
    verify(con, times(2)).close();
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) InOrder(org.mockito.InOrder) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 80 with TransactionStatus

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

the class DataSourceTransactionManagerTests method doTestExistingTransactionWithPropagationNested.

private void doTestExistingTransactionWithPropagationNested(final int count) throws Exception {
    DatabaseMetaData md = mock(DatabaseMetaData.class);
    Savepoint sp = mock(Savepoint.class);
    given(md.supportsSavepoints()).willReturn(true);
    given(con.getMetaData()).willReturn(md);
    for (int i = 1; i <= count; i++) {
        given(con.setSavepoint(ConnectionHolder.SAVEPOINT_NAME_PREFIX + i)).willReturn(sp);
    }
    final TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
    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());
            assertTrue("Isn't nested transaction", !status.hasSavepoint());
            for (int i = 0; i < count; i++) {
                tt.execute(new TransactionCallbackWithoutResult() {

                    @Override
                    protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                        assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
                        assertTrue("Synchronization active", TransactionSynchronizationManager.isSynchronizationActive());
                        assertTrue("Isn't new transaction", !status.isNewTransaction());
                        assertTrue("Is nested transaction", status.hasSavepoint());
                    }
                });
            }
            assertTrue("Is new transaction", status.isNewTransaction());
            assertTrue("Isn't nested transaction", !status.hasSavepoint());
        }
    });
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    verify(con, times(count)).releaseSavepoint(sp);
    verify(con).commit();
    verify(con).close();
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) Savepoint(java.sql.Savepoint) DatabaseMetaData(java.sql.DatabaseMetaData) Savepoint(java.sql.Savepoint) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

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