Search in sources :

Example 1 with TransactionManager

use of jakarta.transaction.TransactionManager in project spring-framework by spring-projects.

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNew.

@Test
public void jtaTransactionManagerWithPropagationRequiresNew() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    Transaction tx = mock(Transaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    given(tm.suspend()).willReturn(tx);
    final JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    tt.setName("txName");
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
            assertThat(TransactionSynchronizationManager.getCurrentTransactionName()).isEqualTo("txName");
            assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
            TransactionTemplate tt2 = new TransactionTemplate(ptm);
            tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
            tt2.setReadOnly(true);
            tt2.setName("txName2");
            tt2.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
                    assertThat(TransactionSynchronizationManager.getCurrentTransactionName()).isEqualTo("txName2");
                    assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isTrue();
                }
            });
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
            assertThat(TransactionSynchronizationManager.getCurrentTransactionName()).isEqualTo("txName");
            assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
        }
    });
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    verify(ut, times(2)).begin();
    verify(ut, times(2)).commit();
    verify(tm).resume(tx);
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) Transaction(jakarta.transaction.Transaction) UserTransaction(jakarta.transaction.UserTransaction) TransactionManager(jakarta.transaction.TransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 2 with TransactionManager

use of jakarta.transaction.TransactionManager in project spring-framework by spring-projects.

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndExisting.

@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExisting() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    Transaction tx = mock(Transaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    given(tm.suspend()).willReturn(tx);
    JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
        }
    });
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    verify(ut).begin();
    verify(ut).commit();
    verify(tm).resume(tx);
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) Transaction(jakarta.transaction.Transaction) UserTransaction(jakarta.transaction.UserTransaction) TransactionManager(jakarta.transaction.TransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 3 with TransactionManager

use of jakarta.transaction.TransactionManager in project spring-framework by spring-projects.

the class JtaTransactionManagerTests method jtaTransactionManagerWithExistingTransactionAndJtaSynchronization.

@Test
public void jtaTransactionManagerWithExistingTransactionAndJtaSynchronization() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    MockJtaTransaction tx = new MockJtaTransaction();
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    given(tm.getTransaction()).willReturn(tx);
    final TransactionSynchronization synch = mock(TransactionSynchronization.class);
    JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
    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();
    assertThat(tx.getSynchronization()).isNotNull();
    tx.getSynchronization().beforeCompletion();
    tx.getSynchronization().afterCompletion(Status.STATUS_ROLLEDBACK);
    verify(ut).setRollbackOnly();
    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) TransactionManager(jakarta.transaction.TransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 4 with TransactionManager

use of jakarta.transaction.TransactionManager in project spring-framework by spring-projects.

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationNotSupported.

@Test
public void jtaTransactionManagerWithPropagationNotSupported() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    Transaction tx = mock(Transaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    given(tm.suspend()).willReturn(tx);
    JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
            status.setRollbackOnly();
        }
    });
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    verify(tm).resume(tx);
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) Transaction(jakarta.transaction.Transaction) UserTransaction(jakarta.transaction.UserTransaction) TransactionManager(jakarta.transaction.TransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 5 with TransactionManager

use of jakarta.transaction.TransactionManager in project spring-framework by spring-projects.

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndExistingWithBeginException.

@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExistingWithBeginException() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    Transaction tx = mock(Transaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    given(tm.suspend()).willReturn(tx);
    willThrow(new SystemException()).given(ut).begin();
    JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    assertThatExceptionOfType(CannotCreateTransactionException.class).isThrownBy(() -> tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
        }
    }));
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    verify(tm).resume(tx);
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) Transaction(jakarta.transaction.Transaction) UserTransaction(jakarta.transaction.UserTransaction) SystemException(jakarta.transaction.SystemException) TransactionManager(jakarta.transaction.TransactionManager) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Aggregations

TransactionManager (jakarta.transaction.TransactionManager)12 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)11 UserTransaction (jakarta.transaction.UserTransaction)10 Test (org.junit.jupiter.api.Test)9 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)9 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)9 Transaction (jakarta.transaction.Transaction)5 SystemException (jakarta.transaction.SystemException)2 ExpectedLookupTemplate (org.springframework.context.testfixture.jndi.ExpectedLookupTemplate)2 SimpleNamingContextBuilder (org.springframework.context.testfixture.jndi.SimpleNamingContextBuilder)1 UserTransactionAdapter (org.springframework.transaction.jta.UserTransactionAdapter)1 AbstractPlatformTransactionManager (org.springframework.transaction.support.AbstractPlatformTransactionManager)1 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)1