Search in sources :

Example 6 with TransactionTemplate

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

the class DataSourceTransactionManagerTests method testParticipatingTransactionWithRollbackOnlyAndInnerSynch.

@Test
public void testParticipatingTransactionWithRollbackOnlyAndInnerSynch() throws Exception {
    tm.setTransactionSynchronization(DataSourceTransactionManager.SYNCHRONIZATION_NEVER);
    DataSourceTransactionManager tm2 = new DataSourceTransactionManager(ds);
    // tm has no synch enabled (used at outer level), tm2 has synch enabled (inner level)
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    final TestTransactionSynchronization synch = new TestTransactionSynchronization(ds, TransactionSynchronization.STATUS_UNKNOWN);
    try {
        assertTrue("Is new transaction", ts.isNewTransaction());
        final TransactionTemplate tt = new TransactionTemplate(tm2);
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                assertTrue("Is existing transaction", !status.isNewTransaction());
                assertFalse("Is not rollback-only", status.isRollbackOnly());
                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("Is existing transaction", !status.isNewTransaction());
                        status.setRollbackOnly();
                    }
                });
                assertTrue("Is existing transaction", !status.isNewTransaction());
                assertTrue("Is rollback-only", status.isRollbackOnly());
                TransactionSynchronizationManager.registerSynchronization(synch);
            }
        });
        tm.commit(ts);
        fail("Should have thrown UnexpectedRollbackException");
    } catch (UnexpectedRollbackException ex) {
    // expected
    }
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    assertFalse(synch.beforeCommitCalled);
    assertTrue(synch.beforeCompletionCalled);
    assertFalse(synch.afterCommitCalled);
    assertTrue(synch.afterCompletionCalled);
    verify(con).rollback();
    verify(con).close();
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 7 with TransactionTemplate

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

the class DataSourceTransactionManagerTests method doTestTransactionWithTimeout.

private void doTestTransactionWithTimeout(int timeout) throws Exception {
    Assume.group(TestGroup.PERFORMANCE);
    PreparedStatement ps = mock(PreparedStatement.class);
    given(con.getAutoCommit()).willReturn(true);
    given(con.prepareStatement("some SQL statement")).willReturn(ps);
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setTimeout(timeout);
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    try {
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                try {
                    Thread.sleep(1500);
                } catch (InterruptedException ex) {
                }
                try {
                    Connection con = DataSourceUtils.getConnection(ds);
                    PreparedStatement ps = con.prepareStatement("some SQL statement");
                    DataSourceUtils.applyTransactionTimeout(ps, ds);
                } catch (SQLException ex) {
                    throw new DataAccessResourceFailureException("", ex);
                }
            }
        });
        if (timeout <= 1) {
            fail("Should have thrown TransactionTimedOutException");
        }
    } catch (TransactionTimedOutException ex) {
        if (timeout <= 1) {
        // expected
        } else {
            throw ex;
        }
    }
    assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
    if (timeout > 1) {
        verify(ps).setQueryTimeout(timeout - 1);
        verify(con).commit();
    } else {
        verify(con).rollback();
    }
    InOrder ordered = inOrder(con);
    ordered.verify(con).setAutoCommit(false);
    ordered.verify(con).setAutoCommit(true);
    verify(con).close();
}
Also used : TransactionTimedOutException(org.springframework.transaction.TransactionTimedOutException) InOrder(org.mockito.InOrder) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) DataAccessResourceFailureException(org.springframework.dao.DataAccessResourceFailureException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) PreparedStatement(java.sql.PreparedStatement) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 8 with TransactionTemplate

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

the class DataSourceTransactionManagerTests method testTransactionWithPropagationNever.

@Test
public void testTransactionWithPropagationNever() throws Exception {
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NEVER);
    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 9 with TransactionTemplate

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

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

Aggregations

TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)142 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)103 Test (org.junit.Test)90 TransactionStatus (org.springframework.transaction.TransactionStatus)71 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)50 UserTransaction (javax.transaction.UserTransaction)43 SQLException (java.sql.SQLException)17 Connection (java.sql.Connection)14 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)13 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)12 DataSource (javax.sql.DataSource)11 TransactionCallback (org.springframework.transaction.support.TransactionCallback)10 PreparedStatement (java.sql.PreparedStatement)9 TransactionManager (javax.transaction.TransactionManager)9 InOrder (org.mockito.InOrder)9 SystemException (javax.transaction.SystemException)8 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)8 TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)8 DatabaseMetaData (java.sql.DatabaseMetaData)6 Savepoint (java.sql.Savepoint)6