Search in sources :

Example 1 with JtaTransactionManager

use of cn.taketoday.transaction.jta.JtaTransactionManager 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 2 with JtaTransactionManager

use of cn.taketoday.transaction.jta.JtaTransactionManager 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 JtaTransactionManager

use of cn.taketoday.transaction.jta.JtaTransactionManager in project today-infrastructure by TAKETODAY.

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndSuspensionNotSupported.

@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndSuspensionNotSupported() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    assertThatExceptionOfType(TransactionSuspensionNotSupportedException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
        }
    }));
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(cn.taketoday.transaction.jta.JtaTransactionManager) TransactionTemplate(cn.taketoday.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(cn.taketoday.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 4 with JtaTransactionManager

use of cn.taketoday.transaction.jta.JtaTransactionManager 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 5 with JtaTransactionManager

use of cn.taketoday.transaction.jta.JtaTransactionManager in project today-infrastructure by TAKETODAY.

the class JtaTransactionManagerTests method jtaTransactionManagerWithRollbackAndSynchronizationNever.

@Test
public void jtaTransactionManagerWithRollbackAndSynchronizationNever() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);
    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    ptm.setTransactionSynchronizationName("SYNCHRONIZATION_NEVER");
    tt.setTimeout(10);
    ptm.afterPropertiesSet();
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    tt.execute(new TransactionCallbackWithoutResult() {

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

Aggregations

JtaTransactionManager (cn.taketoday.transaction.jta.JtaTransactionManager)110 TransactionCallbackWithoutResult (cn.taketoday.transaction.support.TransactionCallbackWithoutResult)100 TransactionTemplate (cn.taketoday.transaction.support.TransactionTemplate)100 UserTransaction (jakarta.transaction.UserTransaction)94 Test (org.junit.jupiter.api.Test)94 TransactionSynchronization (cn.taketoday.transaction.support.TransactionSynchronization)40 TransactionManager (jakarta.transaction.TransactionManager)22 SystemException (jakarta.transaction.SystemException)16 TransactionStatus (cn.taketoday.transaction.TransactionStatus)14 Connection (java.sql.Connection)14 Transaction (jakarta.transaction.Transaction)10 DefaultTransactionDefinition (cn.taketoday.transaction.support.DefaultTransactionDefinition)8 DataSource (javax.sql.DataSource)8 ExpectedLookupTemplate (cn.taketoday.testfixture.jndi.ExpectedLookupTemplate)6 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)6 JtaTransactionObject (cn.taketoday.transaction.jta.JtaTransactionObject)4 HeuristicRollbackException (jakarta.transaction.HeuristicRollbackException)4 SQLException (java.sql.SQLException)4 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)2 OptimisticLockingFailureException (cn.taketoday.dao.OptimisticLockingFailureException)2