Search in sources :

Example 41 with UserTransaction

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithExistingTransactionAndSynchronizationNever.

@Test
public void jtaTransactionManagerWithExistingTransactionAndSynchronizationNever() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_NEVER);
    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).setRollbackOnly();
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) 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 42 with UserTransaction

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithHeuristicMixedExceptionOnCommit.

@Test
public void jtaTransactionManagerWithHeuristicMixedExceptionOnCommit() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    willThrow(new HeuristicMixedException("heuristic exception")).given(ut).commit();
    assertThatExceptionOfType(HeuristicCompletionException.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_UNKNOWN).as("Correct completion status").isTrue();
                    }
                });
            }
        });
    }).satisfies(ex -> assertThat(ex.getOutcomeState()).isEqualTo(HeuristicCompletionException.STATE_MIXED));
    verify(ut).begin();
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) HeuristicMixedException(jakarta.transaction.HeuristicMixedException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.jupiter.api.Test)

Example 43 with UserTransaction

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithRollback.

@Test
public void jtaTransactionManagerWithRollback() 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);
    tt.setTimeout(10);
    tt.setName("txName");
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    assertThat(TransactionSynchronizationManager.getCurrentTransactionName()).isNull();
    assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
            TransactionSynchronizationManager.registerSynchronization(synch);
            assertThat(TransactionSynchronizationManager.getCurrentTransactionName()).isEqualTo("txName");
            assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
            status.setRollbackOnly();
        }
    });
    assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isFalse();
    assertThat(TransactionSynchronizationManager.getCurrentTransactionName()).isNull();
    assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).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(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 44 with UserTransaction

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationSupportsAndSynchronizationOnActual.

@Test
public void jtaTransactionManagerWithPropagationSupportsAndSynchronizationOnActual() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);
    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    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();
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) 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 45 with UserTransaction

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

the class JtaTransactionManager method findUserTransaction.

/**
 * Find the JTA UserTransaction through a default JNDI lookup:
 * "java:comp/UserTransaction".
 * @return the JTA UserTransaction reference, or {@code null} if not found
 * @see #DEFAULT_USER_TRANSACTION_NAME
 */
@Nullable
protected UserTransaction findUserTransaction() {
    String jndiName = DEFAULT_USER_TRANSACTION_NAME;
    try {
        UserTransaction ut = getJndiTemplate().lookup(jndiName, UserTransaction.class);
        if (logger.isDebugEnabled()) {
            logger.debug("JTA UserTransaction found at default JNDI location [" + jndiName + "]");
        }
        this.userTransactionObtainedFromJndi = true;
        return ut;
    } catch (NamingException ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("No JTA UserTransaction found at default JNDI location [" + jndiName + "]", ex);
        }
        return null;
    }
}
Also used : UserTransaction(jakarta.transaction.UserTransaction) NamingException(javax.naming.NamingException) Nullable(org.springframework.lang.Nullable)

Aggregations

UserTransaction (jakarta.transaction.UserTransaction)47 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)46 Test (org.junit.jupiter.api.Test)44 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)42 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)42 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)20 TransactionManager (jakarta.transaction.TransactionManager)9 SystemException (jakarta.transaction.SystemException)7 Transaction (jakarta.transaction.Transaction)4 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)4 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)3 ExpectedLookupTemplate (org.springframework.context.testfixture.jndi.ExpectedLookupTemplate)3 HeuristicRollbackException (jakarta.transaction.HeuristicRollbackException)2 HeuristicMixedException (jakarta.transaction.HeuristicMixedException)1 NotSupportedException (jakarta.transaction.NotSupportedException)1 RollbackException (jakarta.transaction.RollbackException)1 NamingException (javax.naming.NamingException)1 SimpleNamingContextBuilder (org.springframework.context.testfixture.jndi.SimpleNamingContextBuilder)1 OptimisticLockingFailureException (org.springframework.dao.OptimisticLockingFailureException)1 Nullable (org.springframework.lang.Nullable)1