Search in sources :

Example 41 with JtaTransactionManager

use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.

the class JtaTransactionManagerTests method jtaTransactionManagerWithExistingAndPropagationSupports.

@Test
public void jtaTransactionManagerWithExistingAndPropagationSupports() 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);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
Also used : UserTransaction(javax.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.Test)

Example 42 with JtaTransactionManager

use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.

the class JtaTransactionManagerTests method jtaTransactionManagerWithDoubleCommit.

@Test
public void jtaTransactionManagerWithDoubleCommit() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    TransactionStatus status = ptm.getTransaction(new DefaultTransactionDefinition());
    assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
    // first commit
    ptm.commit(status);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    try {
        // second commit attempt
        ptm.commit(status);
        fail("Should have thrown IllegalTransactionStateException");
    } catch (IllegalTransactionStateException ex) {
    // expected
    }
    verify(ut).begin();
    verify(ut).commit();
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Test(org.junit.Test)

Example 43 with JtaTransactionManager

use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewWithinSupports.

@Test
public void jtaTransactionManagerWithPropagationRequiresNewWithinSupports() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    final JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
            assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
            TransactionTemplate tt2 = new TransactionTemplate(ptm);
            tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
            tt2.execute(new TransactionCallbackWithoutResult() {

                @Override
                protected void doInTransactionWithoutResult(TransactionStatus status) {
                    assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
                    assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
                }
            });
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
            assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    verify(ut).begin();
    verify(ut).commit();
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 44 with JtaTransactionManager

use of org.springframework.transaction.jta.JtaTransactionManager in project spring-framework by spring-projects.

the class JtaTransactionManagerTests method jtaTransactionManagerWithSystemExceptionOnRollbackOnly.

@Test
public void jtaTransactionManagerWithSystemExceptionOnRollbackOnly() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    willThrow(new SystemException("system exception")).given(ut).setRollbackOnly();
    try {
        JtaTransactionManager ptm = newJtaTransactionManager(ut);
        TransactionTemplate tt = new TransactionTemplate(ptm);
        tt.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                status.setRollbackOnly();
                TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

                    @Override
                    public void afterCompletion(int status) {
                        assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_UNKNOWN);
                    }
                });
            }
        });
        fail("Should have thrown TransactionSystemException");
    } catch (TransactionSystemException ex) {
    // expected
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) SystemException(javax.transaction.SystemException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Test(org.junit.Test)

Example 45 with JtaTransactionManager

use of org.springframework.transaction.jta.JtaTransactionManager 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();
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    verify(ut).setRollbackOnly();
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Aggregations

JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)57 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)50 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)50 Test (org.junit.Test)49 UserTransaction (javax.transaction.UserTransaction)47 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)13 TransactionManager (javax.transaction.TransactionManager)11 SystemException (javax.transaction.SystemException)8 Connection (java.sql.Connection)7 TransactionStatus (org.springframework.transaction.TransactionStatus)7 TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)7 MockJtaTransaction (org.springframework.tests.transaction.MockJtaTransaction)6 Transaction (javax.transaction.Transaction)5 DataSource (javax.sql.DataSource)4 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)4 ExpectedLookupTemplate (org.springframework.tests.mock.jndi.ExpectedLookupTemplate)3 SQLException (java.sql.SQLException)2 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 JtaTransactionObject (org.springframework.transaction.jta.JtaTransactionObject)2