Search in sources :

Example 1 with DefaultTransactionDefinition

use of cn.taketoday.transaction.support.DefaultTransactionDefinition in project today-infrastructure by TAKETODAY.

the class JdbcTransactionManagerTests method doTestParticipatingTransactionWithRollbackOnly.

private void doTestParticipatingTransactionWithRollbackOnly(boolean failEarly) throws Exception {
    given(con.isReadOnly()).willReturn(false);
    if (failEarly) {
        tm.setFailEarlyOnGlobalRollbackOnly(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();
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    TestTransactionSynchronization synch = new TestTransactionSynchronization(ds, TransactionSynchronization.STATUS_ROLLED_BACK);
    TransactionSynchronizationManager.registerSynchronization(synch);
    boolean outerTransactionBoundaryReached = false;
    try {
        assertThat(ts.isNewTransaction()).as("Is new transaction").isTrue();
        final TransactionTemplate tt = new TransactionTemplate(tm);
        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();
            }
        });
        outerTransactionBoundaryReached = true;
        tm.commit(ts);
        fail("Should have thrown UnexpectedRollbackException");
    } catch (UnexpectedRollbackException ex) {
        // expected
        if (!outerTransactionBoundaryReached) {
            tm.rollback(ts);
        }
        if (failEarly) {
            assertThat(outerTransactionBoundaryReached).isFalse();
        } else {
            assertThat(outerTransactionBoundaryReached).isTrue();
        }
    }
    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(cn.taketoday.transaction.support.DefaultTransactionDefinition) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionStatus(cn.taketoday.transaction.TransactionStatus) UnexpectedRollbackException(cn.taketoday.transaction.UnexpectedRollbackException) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult)

Example 2 with DefaultTransactionDefinition

use of cn.taketoday.transaction.support.DefaultTransactionDefinition in project today-infrastructure by TAKETODAY.

the class JdbcTransactionManagerTests method testParticipatingTransactionWithRollbackOnlyAndInnerSynch.

@Test
public void testParticipatingTransactionWithRollbackOnlyAndInnerSynch() throws Exception {
    tm.setTransactionSynchronization(JdbcTransactionManager.SYNCHRONIZATION_NEVER);
    JdbcTransactionManager tm2 = new JdbcTransactionManager(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(cn.taketoday.transaction.support.DefaultTransactionDefinition) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionStatus(cn.taketoday.transaction.TransactionStatus) UnexpectedRollbackException(cn.taketoday.transaction.UnexpectedRollbackException) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with DefaultTransactionDefinition

use of cn.taketoday.transaction.support.DefaultTransactionDefinition in project today-infrastructure by TAKETODAY.

the class JtaTransactionManagerTests method jtaTransactionManagerWithRollbackAndCommit.

@Test
public void jtaTransactionManagerWithRollbackAndCommit() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);
    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    TransactionStatus status = ptm.getTransaction(new DefaultTransactionDefinition());
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
    // first: rollback
    ptm.rollback(status);
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    // second: commit attempt
    assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() -> ptm.commit(status));
    verify(ut).begin();
    verify(ut).rollback();
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(cn.taketoday.transaction.jta.JtaTransactionManager) DefaultTransactionDefinition(cn.taketoday.transaction.support.DefaultTransactionDefinition) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultTransactionDefinition

use of cn.taketoday.transaction.support.DefaultTransactionDefinition in project today-infrastructure by TAKETODAY.

the class JtaTransactionManagerTests method doTestJtaTransactionManagerWithNoExceptionOnGlobalRollbackOnly.

private void doTestJtaTransactionManagerWithNoExceptionOnGlobalRollbackOnly(boolean failEarly) throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_MARKED_ROLLBACK, Status.STATUS_MARKED_ROLLBACK, Status.STATUS_MARKED_ROLLBACK);
    JtaTransactionManager tm = newJtaTransactionManager(ut);
    if (failEarly) {
        tm.setFailEarlyOnGlobalRollbackOnly(true);
    }
    TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
    boolean outerTransactionBoundaryReached = false;
    try {
        assertThat(ts.isNewTransaction()).as("Is new transaction").isTrue();
        TransactionTemplate tt = new TransactionTemplate(tm);
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                // something transactional
                TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {

                    @Override
                    public void afterCompletion(int status) {
                        assertThat(status == TransactionSynchronization.STATUS_ROLLED_BACK).as("Correct completion status").isTrue();
                    }
                });
            }
        });
        outerTransactionBoundaryReached = true;
        tm.commit(ts);
        fail("Should have thrown UnexpectedRollbackException");
    } catch (UnexpectedRollbackException ex) {
        // expected
        if (!outerTransactionBoundaryReached) {
            tm.rollback(ts);
        }
        assertThat(outerTransactionBoundaryReached).isNotEqualTo(failEarly);
    }
    verify(ut).begin();
    if (failEarly) {
        verify(ut).rollback();
    } else {
        verify(ut).commit();
    }
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(cn.taketoday.transaction.jta.JtaTransactionManager) DefaultTransactionDefinition(cn.taketoday.transaction.support.DefaultTransactionDefinition) TransactionSynchronization(cn.taketoday.transaction.support.TransactionSynchronization) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult)

Example 5 with DefaultTransactionDefinition

use of cn.taketoday.transaction.support.DefaultTransactionDefinition in project today-infrastructure by TAKETODAY.

the class JtaTransactionManagerTests method jtaTransactionManagerWithDoubleRollback.

@Test
public void jtaTransactionManagerWithDoubleRollback() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);
    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    TransactionStatus status = ptm.getTransaction(new DefaultTransactionDefinition());
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
    // first rollback
    ptm.rollback(status);
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    // second rollback attempt
    assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() -> ptm.rollback(status));
    verify(ut).begin();
    verify(ut).rollback();
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(cn.taketoday.transaction.jta.JtaTransactionManager) DefaultTransactionDefinition(cn.taketoday.transaction.support.DefaultTransactionDefinition) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultTransactionDefinition (cn.taketoday.transaction.support.DefaultTransactionDefinition)104 Test (org.junit.jupiter.api.Test)98 TransactionStatus (cn.taketoday.transaction.TransactionStatus)58 StepVerifier (reactor.test.StepVerifier)18 TransactionCallbackWithoutResult (cn.taketoday.transaction.support.TransactionCallbackWithoutResult)10 TransactionTemplate (cn.taketoday.transaction.support.TransactionTemplate)10 Environment (org.apache.ibatis.mapping.Environment)10 JdbcTransactionFactory (org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory)10 AbstractMyBatisTest (cn.taketoday.orm.mybatis.AbstractMyBatisTest)8 UnexpectedRollbackException (cn.taketoday.transaction.UnexpectedRollbackException)8 JtaTransactionManager (cn.taketoday.transaction.jta.JtaTransactionManager)8 UserTransaction (jakarta.transaction.UserTransaction)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 Flux (reactor.core.publisher.Flux)8 Mono (reactor.core.publisher.Mono)8 TransactionDefinition (cn.taketoday.transaction.TransactionDefinition)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 SqlSession (org.apache.ibatis.session.SqlSession)6 MockConnection (com.mockrunner.mock.jdbc.MockConnection)5 MockDataSource (com.mockrunner.mock.jdbc.MockDataSource)5