Search in sources :

Example 21 with JtaTransactionManager

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

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            }
        });
        fail("Should have thrown CannotCreateTransactionException");
    } catch (CannotCreateTransactionException ex) {
    // expected
    }
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    verify(tm).resume(tx);
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) Transaction(javax.transaction.Transaction) MockJtaTransaction(org.springframework.tests.transaction.MockJtaTransaction) UserTransaction(javax.transaction.UserTransaction) SystemException(javax.transaction.SystemException) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 22 with JtaTransactionManager

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

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();
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    verify(ut).setTransactionTimeout(10);
    verify(ut).begin();
    verify(ut, atLeastOnce()).getStatus();
    verify(ut).rollback();
}
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 23 with JtaTransactionManager

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

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertNotNull(tx.getSynchronization());
    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(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) MockJtaTransaction(org.springframework.tests.transaction.MockJtaTransaction) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 24 with JtaTransactionManager

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithExistingTransactionAndException.

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

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                TransactionSynchronizationManager.registerSynchronization(synch);
                throw new IllegalStateException("I want a rollback");
            }
        });
        fail("Should have thrown IllegalStateException");
    } catch (IllegalStateException ex) {
    // expected
    }
    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 25 with JtaTransactionManager

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithSystemExceptionOnIsExisting.

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

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            }
        });
        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) 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