Search in sources :

Example 81 with TransactionCallbackWithoutResult

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

the class JdbcTransactionManagerTests method testParticipatingTransactionWithIncompatibleReadOnly.

@Test
public void testParticipatingTransactionWithIncompatibleReadOnly() throws Exception {
    willThrow(new SQLException("read-only not supported")).given(con).setReadOnly(true);
    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);
        tt.setReadOnly(true);
        final TransactionTemplate tt2 = new TransactionTemplate(tm);
        tt2.setReadOnly(false);
        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 : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) 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 82 with TransactionCallbackWithoutResult

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

the class JdbcTransactionManagerTests method testPropagationNotSupportedWithExistingTransaction.

@Test
public void testPropagationNotSupportedWithExistingTransaction() 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();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
            assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
            assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
            assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isTrue();
            tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
            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();
                    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
                    boolean condition = !status.isNewTransaction();
                    assertThat(condition).as("Isn't new transaction").isTrue();
                    assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
                    assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isFalse();
                    status.setRollbackOnly();
                }
            });
            assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
            assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
            assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isTrue();
        }
    });
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    verify(con).commit();
    verify(con).close();
}
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)

Example 83 with TransactionCallbackWithoutResult

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

the class JdbcTransactionManagerTests method testTransactionWithPropagationSupports.

@Test
public void testTransactionWithPropagationSupports() throws Exception {
    TransactionTemplate tt = new TransactionTemplate(tm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    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();
            assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
            assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isFalse();
        }
    });
    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)

Example 84 with TransactionCallbackWithoutResult

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

the class JdbcTransactionManagerTests 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);
    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();
            boolean condition1 = !status.hasSavepoint();
            assertThat(condition1).as("Isn't nested transaction").isTrue();
            for (int i = 0; i < count; i++) {
                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("Isn't new transaction").isTrue();
                        assertThat(status.hasSavepoint()).as("Is nested transaction").isTrue();
                    }
                });
            }
            assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
            boolean condition = !status.hasSavepoint();
            assertThat(condition).as("Isn't nested transaction").isTrue();
        }
    });
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    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)

Example 85 with TransactionCallbackWithoutResult

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

the class JdbcTransactionManagerTests method testTransactionWithDataAccessExceptionOnCommitFromLazyExceptionTranslator.

@Test
public void testTransactionWithDataAccessExceptionOnCommitFromLazyExceptionTranslator() throws Exception {
    willThrow(new SQLException("Cannot commit", "40")).given(con).commit();
    TransactionTemplate tt = new TransactionTemplate(tm);
    assertThatExceptionOfType(ConcurrencyFailureException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
        // something transactional
        }
    }));
    boolean condition = !TransactionSynchronizationManager.hasResource(ds);
    assertThat(condition).as("Hasn't thread connection").isTrue();
    verify(con).close();
}
Also used : UncategorizedSQLException(org.springframework.jdbc.UncategorizedSQLException) SQLException(java.sql.SQLException) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) 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

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