Search in sources :

Example 81 with TransactionTemplate

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

the class JdbcTransactionManagerTests method testParticipatingTransactionWithDifferentConnectionObtainedFromSynch.

@Test
public void testParticipatingTransactionWithDifferentConnectionObtainedFromSynch() throws Exception {
    DataSource ds2 = mock(DataSource.class);
    final Connection con2 = mock(Connection.class);
    given(ds2.getConnection()).willReturn(con2);
    boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition2).as("Hasn't thread connection").isTrue();
    boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition1).as("Synchronization not active").isTrue();
    final TransactionTemplate tt = new TransactionTemplate(tm);
    final TestTransactionSynchronization synch = new TestTransactionSynchronization(ds, TransactionSynchronization.STATUS_COMMITTED) {

        @Override
        protected void doAfterCompletion(int status) {
            super.doAfterCompletion(status);
            Connection con = DataSourceUtils.getConnection(ds2);
            DataSourceUtils.releaseConnection(con, ds2);
        }
    };
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            TransactionSynchronizationManager.registerSynchronization(synch);
        }
    });
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    assertThat(synch.beforeCommitCalled).isTrue();
    assertThat(synch.beforeCompletionCalled).isTrue();
    assertThat(synch.afterCommitCalled).isTrue();
    assertThat(synch.afterCompletionCalled).isTrue();
    assertThat(synch.afterCompletionException).isNull();
    verify(con).commit();
    verify(con).close();
    verify(con2).close();
}
Also used : Connection(java.sql.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) DataSource(javax.sql.DataSource) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 82 with TransactionTemplate

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

the class JdbcTransactionManagerTests 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);
    boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition1).as("Hasn't thread connection").isTrue();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
            final TransactionAwareDataSourceProxy dsProxy = new TransactionAwareDataSourceProxy(ds);
            dsProxy.setReobtainTransactionalConnections(true);
            try {
                assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
                // 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
                    assertThat(DataSourceUtils.getConnection(ds)).isEqualTo(con);
                    try {
                        assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
                        // should be ignored
                        dsProxy.getConnection().close();
                    } catch (SQLException ex) {
                        throw new UncategorizedSQLException("", "", ex);
                    }
                }
            });
            try {
                assertThat(((ConnectionProxy) dsProxy.getConnection()).getTargetConnection()).isEqualTo(con);
                // should be ignored
                dsProxy.getConnection().close();
            } catch (SQLException ex) {
                throw new UncategorizedSQLException("", "", ex);
            }
        }
    });
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    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) TransactionAwareDataSourceProxy(org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy) UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) ConnectionProxy(org.springframework.jdbc.datasource.ConnectionProxy) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 83 with TransactionTemplate

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

the class JdbcTransactionManagerTests 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);
    boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition2).as("Hasn't thread connection").isTrue();
    boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition1).as("Synchronization not active").isTrue();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
            Object savepoint = status.createSavepoint();
            status.releaseSavepoint(savepoint);
            assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
        }
    });
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    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.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 84 with TransactionTemplate

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

the class JdbcTransactionManagerTests method testPropagationSupportsAndRequiresNewWithEarlyAccess.

@Test
public void testPropagationSupportsAndRequiresNewWithEarlyAccess() throws Exception {
    final Connection con1 = mock(Connection.class);
    final Connection con2 = mock(Connection.class);
    given(ds.getConnection()).willReturn(con1, con2);
    final TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition2).as("Hasn't thread connection").isTrue();
    boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition1).as("Synchronization not active").isTrue();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
            assertThat(DataSourceUtils.getConnection(ds)).isSameAs(con1);
            assertThat(DataSourceUtils.getConnection(ds)).isSameAs(con1);
            TransactionTemplate tt2 = new TransactionTemplate(tm);
            tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
            tt2.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                    assertThat(TransactionSynchronizationManager.hasResource(ds)).as("Has thread connection").isTrue();
                    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
                    assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
                    assertThat(DataSourceUtils.getConnection(ds)).isSameAs(con2);
                    assertThat(DataSourceUtils.getConnection(ds)).isSameAs(con2);
                }
            });
            assertThat(DataSourceUtils.getConnection(ds)).isSameAs(con1);
        }
    });
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    verify(con1).close();
    verify(con2).commit();
    verify(con2).close();
}
Also used : Connection(java.sql.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 85 with TransactionTemplate

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

the class JdbcTransactionManagerTests method testTransactionWithPropagationNever.

@Test
public void testTransactionWithPropagationNever() throws Exception {
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NEVER);
    boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition1).as("Hasn't thread connection").isTrue();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
            assertThat(condition1).as("Hasn't thread connection").isTrue();
            boolean condition = !status.isNewTransaction();
            assertThat(condition).as("Is not new transaction").isTrue();
        }
    });
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)287 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)178 TransactionStatus (org.springframework.transaction.TransactionStatus)158 Test (org.junit.jupiter.api.Test)139 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)70 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)56 UserTransaction (jakarta.transaction.UserTransaction)43 SQLException (java.sql.SQLException)36 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)29 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)25 Connection (java.sql.Connection)23 Test (org.junit.Test)22 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)22 InOrder (org.mockito.InOrder)21 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)18 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)17 DataSource (javax.sql.DataSource)16 DatabaseMetaData (java.sql.DatabaseMetaData)12 PreparedStatement (java.sql.PreparedStatement)12 Savepoint (java.sql.Savepoint)12