Search in sources :

Example 1 with TransactionSynchronization

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

the class JdbcTransactionManagerTests method testParticipatingTransactionWithTransactionStartedFromSynch.

@Test
public void testParticipatingTransactionWithTransactionStartedFromSynch() throws Exception {
    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);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    final TestTransactionSynchronization synch = new TestTransactionSynchronization(ds, TransactionSynchronization.STATUS_COMMITTED) {

        @Override
        protected void doAfterCompletion(int status) {
            super.doAfterCompletion(status);
            tt.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                }
            });
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            });
        }
    };
    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();
    boolean condition3 = synch.afterCompletionException instanceof IllegalStateException;
    assertThat(condition3).isTrue();
    verify(con, times(2)).commit();
    verify(con, times(2)).close();
}
Also used : Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) TransactionSynchronization(cn.taketoday.transaction.support.TransactionSynchronization) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionStatus(cn.taketoday.transaction.TransactionStatus) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with TransactionSynchronization

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithRollbackAndSynchronizationOnActual.

@Test
public void jtaTransactionManagerWithRollbackAndSynchronizationOnActual() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);
    final TransactionSynchronization synch = mock(TransactionSynchronization.class);
    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
    tt.setTimeout(10);
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    verify(ut).setTransactionTimeout(10);
    verify(ut).begin();
    verify(ut).rollback();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(cn.taketoday.transaction.jta.JtaTransactionManager) TransactionSynchronization(cn.taketoday.transaction.support.TransactionSynchronization) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 3 with TransactionSynchronization

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithExistingTransactionAndExceptionAndNoGlobalRollback.

@Test
public void jtaTransactionManagerWithExistingTransactionAndExceptionAndNoGlobalRollback() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    final TransactionSynchronization synch = mock(TransactionSynchronization.class);
    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    ptm.setGlobalRollbackOnParticipationFailure(false);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    assertThatIllegalStateException().isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
            TransactionSynchronizationManager.registerSynchronization(synch);
            throw new IllegalStateException("I want a rollback");
        }
    }));
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) JtaTransactionManager(cn.taketoday.transaction.jta.JtaTransactionManager) TransactionSynchronization(cn.taketoday.transaction.support.TransactionSynchronization) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 4 with TransactionSynchronization

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithRollbackExceptionOnCommit.

@Test
public void jtaTransactionManagerWithRollbackExceptionOnCommit() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    willThrow(new RollbackException("unexpected rollback")).given(ut).commit();
    assertThatExceptionOfType(UnexpectedRollbackException.class).isThrownBy(() -> {
        JtaTransactionManager ptm = newJtaTransactionManager(ut);
        TransactionTemplate tt = new TransactionTemplate(ptm);
        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();
                    }
                });
            }
        });
    });
    verify(ut).begin();
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(cn.taketoday.transaction.jta.JtaTransactionManager) TransactionSynchronization(cn.taketoday.transaction.support.TransactionSynchronization) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) HeuristicRollbackException(jakarta.transaction.HeuristicRollbackException) RollbackException(jakarta.transaction.RollbackException) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 5 with TransactionSynchronization

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithExistingTransactionAndSynchronizationOnActual.

@Test
public void jtaTransactionManagerWithExistingTransactionAndSynchronizationOnActual() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    final TransactionSynchronization synch = mock(TransactionSynchronization.class);
    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(cn.taketoday.transaction.jta.JtaTransactionManager) TransactionSynchronization(cn.taketoday.transaction.support.TransactionSynchronization) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Aggregations

TransactionCallbackWithoutResult (cn.taketoday.transaction.support.TransactionCallbackWithoutResult)46 TransactionSynchronization (cn.taketoday.transaction.support.TransactionSynchronization)46 TransactionTemplate (cn.taketoday.transaction.support.TransactionTemplate)46 Test (org.junit.jupiter.api.Test)44 JtaTransactionManager (cn.taketoday.transaction.jta.JtaTransactionManager)40 UserTransaction (jakarta.transaction.UserTransaction)40 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)8 TransactionStatus (cn.taketoday.transaction.TransactionStatus)6 SystemException (jakarta.transaction.SystemException)6 HeuristicRollbackException (jakarta.transaction.HeuristicRollbackException)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 OptimisticLockingFailureException (cn.taketoday.dao.OptimisticLockingFailureException)2 InvalidIsolationLevelException (cn.taketoday.transaction.InvalidIsolationLevelException)2 TransactionDefinition (cn.taketoday.transaction.TransactionDefinition)2 TransactionSystemException (cn.taketoday.transaction.TransactionSystemException)2 DefaultTransactionDefinition (cn.taketoday.transaction.support.DefaultTransactionDefinition)2 TransactionSynchronizationManager (cn.taketoday.transaction.support.TransactionSynchronizationManager)2 EntityManager (jakarta.persistence.EntityManager)2 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)2 EntityTransaction (jakarta.persistence.EntityTransaction)2