Search in sources :

Example 76 with TransactionCallbackWithoutResult

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

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

Example 79 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult 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)
    boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition2).as("Hasn't thread connection").isTrue();
    boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
    assertThat(condition1).as("Synchronization not active").isTrue();
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    final TestTransactionSynchronization synch = new TestTransactionSynchronization(ds, TransactionSynchronization.STATUS_UNKNOWN);
    assertThatExceptionOfType(UnexpectedRollbackException.class).isThrownBy(() -> {
        assertThat(ts.isNewTransaction()).as("Is new transaction").isTrue();
        final TransactionTemplate tt = new TransactionTemplate(tm2);
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
                boolean condition1 = !status.isNewTransaction();
                assertThat(condition1).as("Is existing transaction").isTrue();
                assertThat(status.isRollbackOnly()).as("Is not rollback-only").isFalse();
                tt.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();
                        boolean condition = !status.isNewTransaction();
                        assertThat(condition).as("Is existing transaction").isTrue();
                        status.setRollbackOnly();
                    }
                });
                boolean condition = !status.isNewTransaction();
                assertThat(condition).as("Is existing transaction").isTrue();
                assertThat(status.isRollbackOnly()).as("Is rollback-only").isTrue();
                TransactionSynchronizationManager.registerSynchronization(synch);
            }
        });
        tm.commit(ts);
    });
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    assertThat(synch.beforeCommitCalled).isFalse();
    assertThat(synch.beforeCompletionCalled).isTrue();
    assertThat(synch.afterCommitCalled).isFalse();
    assertThat(synch.afterCompletionCalled).isTrue();
    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.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 80 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult 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);
    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(() -> tt.execute(new TransactionCallbackWithoutResult() {

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

Aggregations

TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)328 TransactionStatus (org.springframework.transaction.TransactionStatus)277 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)179 Test (org.junit.jupiter.api.Test)158 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)70 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)54 UserTransaction (jakarta.transaction.UserTransaction)43 SQLException (java.sql.SQLException)37 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)29 Test (org.junit.Test)25 Connection (java.sql.Connection)23 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)22 InOrder (org.mockito.InOrder)21 Date (java.util.Date)18 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)18 List (java.util.List)16 DataSource (javax.sql.DataSource)15 ArrayList (java.util.ArrayList)14 ManagerException (com.alibaba.otter.manager.biz.common.exceptions.ManagerException)13 RepeatConfigureException (com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException)13