Search in sources :

Example 6 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult 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 7 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult 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 8 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult 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 9 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult 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 10 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult 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

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