Search in sources :

Example 11 with TransactionSynchronization

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithCommit.

@Test
public void jtaTransactionManagerWithCommit() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);
    final TransactionSynchronization synch = mock(TransactionSynchronization.class);
    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setName("txName");
    assertEquals(JtaTransactionManager.SYNCHRONIZATION_ALWAYS, ptm.getTransactionSynchronization());
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertNull(TransactionSynchronizationManager.getCurrentTransactionName());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    tt.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            assertEquals("txName", TransactionSynchronizationManager.getCurrentTransactionName());
            assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertNull(TransactionSynchronizationManager.getCurrentTransactionName());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    verify(ut).begin();
    verify(ut).commit();
    verify(synch).beforeCommit(false);
    verify(synch).beforeCompletion();
    verify(synch).afterCommit();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
}
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 12 with TransactionSynchronization

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

the class JtaTransactionManagerTests method jtaTransactionManagerWithExistingTransactionAndCommitException.

@Test
public void jtaTransactionManagerWithExistingTransactionAndCommitException() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    final TransactionSynchronization synch = mock(TransactionSynchronization.class);
    willThrow(new OptimisticLockingFailureException("")).given(synch).beforeCommit(false);
    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);
            }
        });
        fail("Should have thrown OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException ex) {
    // expected
    }
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
Also used : UserTransaction(javax.transaction.UserTransaction) OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) 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 13 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);
    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(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 14 with TransactionSynchronization

use of org.springframework.transaction.support.TransactionSynchronization 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 15 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);
    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)

Aggregations

TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)18 Test (org.junit.Test)16 UserTransaction (javax.transaction.UserTransaction)13 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)13 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)13 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)13 TransactionSynchronizationManager (com.thoughtworks.go.server.transaction.TransactionSynchronizationManager)2 TransactionStatus (org.springframework.transaction.TransactionStatus)2 Connection (javax.jms.Connection)1 JMSException (javax.jms.JMSException)1 Session (javax.jms.Session)1 TransactionManager (javax.transaction.TransactionManager)1 OptimisticLockingFailureException (org.springframework.dao.OptimisticLockingFailureException)1 SingleConnectionFactory (org.springframework.jms.connection.SingleConnectionFactory)1 TransactionAwareConnectionFactoryProxy (org.springframework.jms.connection.TransactionAwareConnectionFactoryProxy)1 MockJtaTransaction (org.springframework.tests.transaction.MockJtaTransaction)1