Search in sources :

Example 66 with TransactionTemplate

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

the class DataSourceJtaTransactionTests method doTestJtaTransactionCommitWithNewTransactionWithinEmptyTransaction.

private void doTestJtaTransactionCommitWithNewTransactionWithinEmptyTransaction(final boolean requiresNew, boolean notSupported) throws Exception {
    if (notSupported) {
        given(userTransaction.getStatus()).willReturn(Status.STATUS_ACTIVE, Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
        given(transactionManager.suspend()).willReturn(transaction);
    } else {
        given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    }
    final DataSource dataSource = mock(DataSource.class);
    final Connection connection1 = mock(Connection.class);
    final Connection connection2 = mock(Connection.class);
    given(dataSource.getConnection()).willReturn(connection1, connection2);
    final JtaTransactionManager ptm = new JtaTransactionManager(userTransaction, transactionManager);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(notSupported ? TransactionDefinition.PROPAGATION_NOT_SUPPORTED : TransactionDefinition.PROPAGATION_SUPPORTS);
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
            assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
            assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isFalse();
            assertThat(DataSourceUtils.getConnection(dataSource)).isSameAs(connection1);
            assertThat(DataSourceUtils.getConnection(dataSource)).isSameAs(connection1);
            TransactionTemplate tt2 = new TransactionTemplate(ptm);
            tt2.setPropagationBehavior(requiresNew ? TransactionDefinition.PROPAGATION_REQUIRES_NEW : TransactionDefinition.PROPAGATION_REQUIRED);
            tt2.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
                    assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
                    assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isTrue();
                    assertThat(DataSourceUtils.getConnection(dataSource)).isSameAs(connection2);
                    assertThat(DataSourceUtils.getConnection(dataSource)).isSameAs(connection2);
                }
            });
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
            assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
            assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isFalse();
            assertThat(DataSourceUtils.getConnection(dataSource)).isSameAs(connection1);
        }
    });
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    verify(userTransaction).begin();
    verify(userTransaction).commit();
    if (notSupported) {
        verify(transactionManager).resume(transaction);
    }
    verify(connection2).close();
    verify(connection1).close();
}
Also used : JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) 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)

Example 67 with TransactionTemplate

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

the class DataSourceJtaTransactionTests method doTestJtaTransactionWithPropagationRequiresNewAndBeginException.

private void doTestJtaTransactionWithPropagationRequiresNewAndBeginException(boolean suspendException, final boolean openOuterConnection, final boolean useTransactionAwareDataSource) throws Exception {
    given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    if (suspendException) {
        given(transactionManager.suspend()).willThrow(new SystemException());
    } else {
        given(transactionManager.suspend()).willReturn(transaction);
        willThrow(new SystemException()).given(userTransaction).begin();
    }
    given(connection.isReadOnly()).willReturn(true);
    final DataSource dsToUse = useTransactionAwareDataSource ? new TransactionAwareDataSourceProxy(dataSource) : dataSource;
    if (dsToUse instanceof TransactionAwareDataSourceProxy) {
        ((TransactionAwareDataSourceProxy) dsToUse).setReobtainTransactionalConnections(true);
    }
    JtaTransactionManager ptm = new JtaTransactionManager(userTransaction, transactionManager);
    final TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    boolean condition3 = !TransactionSynchronizationManager.hasResource(dsToUse);
    assertThat(condition3).as("Hasn't thread connection").isTrue();
    boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition2).as("JTA synchronizations not active").isTrue();
    assertThatExceptionOfType(TransactionException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            boolean condition = !TransactionSynchronizationManager.hasResource(dsToUse);
            assertThat(condition).as("Hasn't thread connection").isTrue();
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("JTA synchronizations active").isTrue();
            assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
            Connection c = DataSourceUtils.getConnection(dsToUse);
            try {
                assertThat(TransactionSynchronizationManager.hasResource(dsToUse)).as("Has thread connection").isTrue();
                c.isReadOnly();
                DataSourceUtils.releaseConnection(c, dsToUse);
                c = DataSourceUtils.getConnection(dsToUse);
                assertThat(TransactionSynchronizationManager.hasResource(dsToUse)).as("Has thread connection").isTrue();
                if (!openOuterConnection) {
                    DataSourceUtils.releaseConnection(c, dsToUse);
                }
            } catch (SQLException ex) {
            }
            try {
                tt.execute(new TransactionCallbackWithoutResult() {

                    @Override
                    protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                        boolean condition = !TransactionSynchronizationManager.hasResource(dsToUse);
                        assertThat(condition).as("Hasn't thread connection").isTrue();
                        assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("JTA synchronizations active").isTrue();
                        assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
                        Connection c = DataSourceUtils.getConnection(dsToUse);
                        assertThat(TransactionSynchronizationManager.hasResource(dsToUse)).as("Has thread connection").isTrue();
                        DataSourceUtils.releaseConnection(c, dsToUse);
                        c = DataSourceUtils.getConnection(dsToUse);
                        assertThat(TransactionSynchronizationManager.hasResource(dsToUse)).as("Has thread connection").isTrue();
                        DataSourceUtils.releaseConnection(c, dsToUse);
                    }
                });
            } finally {
                if (openOuterConnection) {
                    try {
                        c.isReadOnly();
                        DataSourceUtils.releaseConnection(c, dsToUse);
                    } catch (SQLException ex) {
                    }
                }
            }
        }
    }));
    boolean condition1 = !TransactionSynchronizationManager.hasResource(dsToUse);
    assertThat(condition1).as("Hasn't thread connection").isTrue();
    boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition).as("JTA synchronizations not active").isTrue();
    verify(userTransaction).begin();
    if (suspendException) {
        verify(userTransaction).rollback();
    }
    if (suspendException) {
        verify(connection, atLeastOnce()).close();
    } else {
        verify(connection, never()).close();
    }
}
Also used : SQLException(java.sql.SQLException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) DataSource(javax.sql.DataSource) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionException(org.springframework.transaction.TransactionException) SystemException(jakarta.transaction.SystemException) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 68 with TransactionTemplate

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

the class DataSourceJtaTransactionTests method testJtaTransactionWithIsolationLevelDataSourceAdapter.

@Test
public void testJtaTransactionWithIsolationLevelDataSourceAdapter() throws Exception {
    given(userTransaction.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    final IsolationLevelDataSourceAdapter dsToUse = new IsolationLevelDataSourceAdapter();
    dsToUse.setTargetDataSource(dataSource);
    dsToUse.afterPropertiesSet();
    JtaTransactionManager ptm = new JtaTransactionManager(userTransaction);
    ptm.setAllowCustomIsolationLevels(true);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            Connection c = DataSourceUtils.getConnection(dsToUse);
            assertThat(TransactionSynchronizationManager.hasResource(dsToUse)).as("Has thread connection").isTrue();
            assertThat(c).isSameAs(connection);
            DataSourceUtils.releaseConnection(c, dsToUse);
        }
    });
    tt.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
    tt.setReadOnly(true);
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            Connection c = DataSourceUtils.getConnection(dsToUse);
            assertThat(TransactionSynchronizationManager.hasResource(dsToUse)).as("Has thread connection").isTrue();
            assertThat(c).isSameAs(connection);
            DataSourceUtils.releaseConnection(c, dsToUse);
        }
    });
    verify(userTransaction, times(2)).begin();
    verify(userTransaction, times(2)).commit();
    verify(connection).setReadOnly(true);
    verify(connection).setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
    verify(connection, times(2)).close();
}
Also used : JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 69 with TransactionTemplate

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

the class DataSourceTransactionManagerTests method testParticipatingTransactionWithIncompatibleIsolationLevel.

@Test
public void testParticipatingTransactionWithIncompatibleIsolationLevel() throws Exception {
    tm.setValidateExistingTransaction(true);
    boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition2).as("Hasn't thread connection").isTrue();
    boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition1).as("Synchronization not active").isTrue();
    assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() -> {
        final TransactionTemplate tt = new TransactionTemplate(tm);
        final TransactionTemplate tt2 = new TransactionTemplate(tm);
        tt2.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                assertThat(status.isRollbackOnly()).as("Is not rollback-only").isFalse();
                tt2.execute(new TransactionCallbackWithoutResult() {

                    @Override
                    protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                        status.setRollbackOnly();
                    }
                });
                assertThat(status.isRollbackOnly()).as("Is rollback-only").isTrue();
            }
        });
    });
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    verify(con).rollback();
    verify(con).close();
}
Also used : IllegalTransactionStateException(org.springframework.transaction.IllegalTransactionStateException) 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 70 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);
    boolean condition4 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition4).as("Hasn't thread connection").isTrue();
    boolean condition3 = !TransactionSynchronizationManager.hasResource(ds2);
    assertThat(condition3).as("Hasn't thread connection").isTrue();
    boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition2).as("Synchronization not active").isTrue();
    assertThatExceptionOfType(CannotCreateTransactionException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {

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

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                    status.setRollbackOnly();
                }
            });
        }
    })).withCause(failure);
    boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition1).as("Hasn't thread connection").isTrue();
    boolean condition = !TransactionSynchronizationManager.hasResource(ds2);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    verify(con).rollback();
    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) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) DataSource(javax.sql.DataSource) 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