Search in sources :

Example 21 with TransactionSynchronization

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationSupports.

@Test
public void jtaTransactionManagerWithPropagationSupports() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);
    final TransactionSynchronization synch = mock(TransactionSynchronization.class);
    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    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(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 22 with TransactionSynchronization

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

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(org.springframework.transaction.jta.JtaTransactionManager) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 23 with TransactionSynchronization

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithSystemExceptionOnRollback.

@Test
public void jtaTransactionManagerWithSystemExceptionOnRollback() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);
    willThrow(new SystemException("system exception")).given(ut).rollback();
    assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() -> {
        JtaTransactionManager ptm = newJtaTransactionManager(ut);
        TransactionTemplate tt = new TransactionTemplate(ptm);
        tt.execute(new TransactionCallbackWithoutResult() {

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

                    @Override
                    public void afterCompletion(int status) {
                        assertThat(status == TransactionSynchronization.STATUS_UNKNOWN).as("Correct completion status").isTrue();
                    }
                });
                status.setRollbackOnly();
            }
        });
    });
    verify(ut).begin();
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) SystemException(jakarta.transaction.SystemException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 24 with TransactionSynchronization

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithExistingTransactionAndRollbackOnly.

@Test
public void jtaTransactionManagerWithExistingTransactionAndRollbackOnly() 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);
    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(org.springframework.transaction.jta.JtaTransactionManager) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 25 with TransactionSynchronization

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

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(org.springframework.transaction.jta.JtaTransactionManager) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Aggregations

TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)32 Test (org.junit.jupiter.api.Test)25 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)23 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)23 UserTransaction (jakarta.transaction.UserTransaction)20 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)20 TransactionStatus (org.springframework.transaction.TransactionStatus)5 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)4 SystemException (jakarta.transaction.SystemException)3 HeuristicRollbackException (jakarta.transaction.HeuristicRollbackException)2 List (java.util.List)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 BDDMockito.given (org.mockito.BDDMockito.given)2 BDDMockito.willThrow (org.mockito.BDDMockito.willThrow)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.verify (org.mockito.Mockito.verify)2 IntegrationResourceHolder (org.springframework.integration.transaction.IntegrationResourceHolder)2