Search in sources :

Example 36 with DefaultTransactionDefinition

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

the class WebSphereUowTransactionManagerTests method doTestNewTransactionWithCommit.

private void doTestNewTransactionWithCommit(int propagationBehavior, final int synchMode) {
    MockUOWManager manager = new MockUOWManager();
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    ptm.setTransactionSynchronization(synchMode);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setPropagationBehavior(propagationBehavior);
    definition.setReadOnly(true);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {

        @Override
        public String doInTransaction(TransactionStatus status) {
            if (synchMode != WebSphereUowTransactionManager.SYNCHRONIZATION_NEVER) {
                assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
                assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
            } else {
                assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
                assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
                assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
            }
            return "result";
        }
    }));
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    assertEquals(0, manager.getUOWTimeout());
    assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
    assertFalse(manager.getJoined());
    assertFalse(manager.getRollbackOnly());
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionStatus(org.springframework.transaction.TransactionStatus)

Example 37 with DefaultTransactionDefinition

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

the class WebSphereUowTransactionManagerTests method existingTransactionUsingPropagationNotSupported.

@Test
public void existingTransactionUsingPropagationNotSupported() {
    MockUOWManager manager = new MockUOWManager();
    final WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    final DefaultTransactionDefinition definition2 = new DefaultTransactionDefinition();
    definition2.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
    definition2.setReadOnly(true);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {

        @Override
        public String doInTransaction(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
            assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
            assertEquals("result2", ptm.execute(definition2, new TransactionCallback<String>() {

                @Override
                public String doInTransaction(TransactionStatus status) {
                    assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
                    assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
                    return "result2";
                }
            }));
            return "result";
        }
    }));
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    assertEquals(0, manager.getUOWTimeout());
    assertEquals(UOWManager.UOW_TYPE_LOCAL_TRANSACTION, manager.getUOWType());
    assertFalse(manager.getJoined());
    assertFalse(manager.getRollbackOnly());
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 38 with DefaultTransactionDefinition

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

the class WebSphereUowTransactionManagerTests method propagationMandatoryFailsInCaseOfNoExistingTransaction.

@Test
public void propagationMandatoryFailsInCaseOfNoExistingTransaction() {
    MockUOWManager manager = new MockUOWManager();
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_MANDATORY);
    try {
        ptm.execute(definition, new TransactionCallback<String>() {

            @Override
            public String doInTransaction(TransactionStatus status) {
                return "result";
            }
        });
        fail("Should have thrown IllegalTransactionStateException");
    } catch (IllegalTransactionStateException ex) {
    // expected
    }
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) IllegalTransactionStateException(org.springframework.transaction.IllegalTransactionStateException) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 39 with DefaultTransactionDefinition

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

the class WebSphereUowTransactionManagerTests method propagationNestedFailsInCaseOfExistingTransaction.

@Test
public void propagationNestedFailsInCaseOfExistingTransaction() {
    MockUOWManager manager = new MockUOWManager();
    manager.setUOWStatus(UOWManager.UOW_STATUS_ACTIVE);
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
    try {
        ptm.execute(definition, new TransactionCallback<String>() {

            @Override
            public String doInTransaction(TransactionStatus status) {
                return "result";
            }
        });
        fail("Should have thrown NestedTransactionNotSupportedException");
    } catch (NestedTransactionNotSupportedException ex) {
    // expected
    }
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) NestedTransactionNotSupportedException(org.springframework.transaction.NestedTransactionNotSupportedException) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 40 with DefaultTransactionDefinition

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

the class WebSphereUowTransactionManagerTests method doTestNewTransactionSynchronization.

private void doTestNewTransactionSynchronization(int propagationBehavior, final int synchMode) {
    MockUOWManager manager = new MockUOWManager();
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    ptm.setTransactionSynchronization(synchMode);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setPropagationBehavior(propagationBehavior);
    definition.setReadOnly(true);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {

        @Override
        public String doInTransaction(TransactionStatus status) {
            if (synchMode == WebSphereUowTransactionManager.SYNCHRONIZATION_ALWAYS) {
                assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
                assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
            } else {
                assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
                assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
                assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
            }
            return "result";
        }
    }));
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    assertEquals(0, manager.getUOWTimeout());
    assertEquals(UOWManager.UOW_TYPE_LOCAL_TRANSACTION, manager.getUOWType());
    assertFalse(manager.getJoined());
    assertFalse(manager.getRollbackOnly());
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionStatus(org.springframework.transaction.TransactionStatus)

Aggregations

DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)45 TransactionStatus (org.springframework.transaction.TransactionStatus)32 Test (org.junit.Test)28 TransactionCallback (org.springframework.transaction.support.TransactionCallback)10 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)8 Connection (javax.jms.Connection)7 ConnectionFactory (javax.jms.ConnectionFactory)7 Session (javax.jms.Session)7 JmsTemplate (org.springframework.jms.core.JmsTemplate)7 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)7 UserTransaction (javax.transaction.UserTransaction)5 SessionCallback (org.springframework.jms.core.SessionCallback)4 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)4 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)4 IllegalTransactionStateException (org.springframework.transaction.IllegalTransactionStateException)3 UOWException (com.ibm.wsspi.uow.UOWException)2 DataAccessException (org.springframework.dao.DataAccessException)2 ExpectedLookupTemplate (org.springframework.tests.mock.jndi.ExpectedLookupTemplate)2 NestedTransactionNotSupportedException (org.springframework.transaction.NestedTransactionNotSupportedException)2 TransactionDefinition (org.springframework.transaction.TransactionDefinition)2