Search in sources :

Example 86 with TransactionStatus

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

the class DataSourceTransactionManagerTests method doTestTransactionRollbackRestoringAutoCommit.

private void doTestTransactionRollbackRestoringAutoCommit(boolean autoCommit, boolean lazyConnection, final boolean createStatement) throws Exception {
    if (lazyConnection) {
        given(con.getAutoCommit()).willReturn(autoCommit);
        given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
    }
    if (!lazyConnection || createStatement) {
        given(con.getAutoCommit()).willReturn(autoCommit);
    }
    final DataSource dsToUse = (lazyConnection ? new LazyConnectionDataSourceProxy(ds) : ds);
    tm = new DataSourceTransactionManager(dsToUse);
    TransactionTemplate tt = new TransactionTemplate(tm);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(dsToUse));
    assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
    final RuntimeException ex = new RuntimeException("Application exception");
    try {
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(dsToUse));
                assertTrue("Synchronization active", TransactionSynchronizationManager.isSynchronizationActive());
                assertTrue("Is new transaction", status.isNewTransaction());
                Connection con = DataSourceUtils.getConnection(dsToUse);
                if (createStatement) {
                    try {
                        con.createStatement();
                    } catch (SQLException ex) {
                        throw new UncategorizedSQLException("", "", ex);
                    }
                }
                throw ex;
            }
        });
        fail("Should have thrown RuntimeException");
    } catch (RuntimeException ex2) {
        // expected
        assertTrue("Correct exception thrown", ex2.equals(ex));
    }
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
    if (autoCommit && (!lazyConnection || createStatement)) {
        InOrder ordered = inOrder(con);
        ordered.verify(con).setAutoCommit(false);
        ordered.verify(con).rollback();
        ordered.verify(con).setAutoCommit(true);
    }
    if (createStatement) {
        verify(con, times(2)).close();
    } else {
        verify(con).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) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) DataSource(javax.sql.DataSource)

Example 87 with TransactionStatus

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

the class DataSourceTransactionManagerTests method testPropagationNeverWithExistingTransaction.

@Test
public void testPropagationNeverWithExistingTransaction() 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());
    try {
        tt.execute(new TransactionCallbackWithoutResult() {

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

                    @Override
                    protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                        fail("Should have thrown IllegalTransactionStateException");
                    }
                });
                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 88 with TransactionStatus

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

the class DataSourceTransactionManagerTests method testExistingTransactionWithManualSavepoint.

@Test
public void testExistingTransactionWithManualSavepoint() throws Exception {
    DatabaseMetaData md = mock(DatabaseMetaData.class);
    Savepoint sp = mock(Savepoint.class);
    given(md.supportsSavepoints()).willReturn(true);
    given(con.getMetaData()).willReturn(md);
    given(con.setSavepoint("SAVEPOINT_1")).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());
            Object savepoint = status.createSavepoint();
            status.releaseSavepoint(savepoint);
            assertTrue("Is new transaction", status.isNewTransaction());
        }
    });
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    verify(con).releaseSavepoint(sp);
    verify(con).commit();
    verify(con).close();
    verify(ds).getConnection();
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) Savepoint(java.sql.Savepoint) DatabaseMetaData(java.sql.DatabaseMetaData) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 89 with TransactionStatus

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

the class DataSourceTransactionManagerTests method testTransactionWithExceptionOnBegin.

/**
	 * Test behavior if the first operation on a connection (getAutoCommit) throws SQLException.
	 */
@Test
public void testTransactionWithExceptionOnBegin() throws Exception {
    willThrow(new SQLException("Cannot begin")).given(con).getAutoCommit();
    TransactionTemplate tt = new TransactionTemplate(tm);
    try {
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            }
        });
        fail("Should have thrown CannotCreateTransactionException");
    } catch (CannotCreateTransactionException ex) {
    // expected
    }
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    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) Test(org.junit.Test)

Example 90 with TransactionStatus

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

the class DataSourceTransactionManagerTests method testParticipatingTransactionWithTransactionStartedFromSynch.

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

        @Override
        protected void doAfterCompletion(int status) {
            super.doAfterCompletion(status);
            tt.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                }
            });
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
            });
        }
    };
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            TransactionSynchronizationManager.registerSynchronization(synch);
        }
    });
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    assertTrue(synch.beforeCommitCalled);
    assertTrue(synch.beforeCompletionCalled);
    assertTrue(synch.afterCommitCalled);
    assertTrue(synch.afterCompletionCalled);
    assertTrue(synch.afterCompletionException instanceof IllegalStateException);
    verify(con, times(2)).commit();
    verify(con, times(2)).close();
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) 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