Search in sources :

Example 61 with TransactionStatus

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

the class CciLocalTransactionTests method testLocalTransactionRollback.

/**
	 * Test if a transaction ( begin / rollback ) is executed on the
	 * LocalTransaction when CciLocalTransactionManager is specified as
	 * transaction manager and a non-checked exception is thrown.
	 */
@Test
public void testLocalTransactionRollback() throws ResourceException {
    final ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    Connection connection = mock(Connection.class);
    Interaction interaction = mock(Interaction.class);
    LocalTransaction localTransaction = mock(LocalTransaction.class);
    final Record record = mock(Record.class);
    final InteractionSpec interactionSpec = mock(InteractionSpec.class);
    given(connectionFactory.getConnection()).willReturn(connection);
    given(connection.getLocalTransaction()).willReturn(localTransaction);
    given(connection.createInteraction()).willReturn(interaction);
    given(interaction.execute(interactionSpec, record, record)).willReturn(true);
    given(connection.getLocalTransaction()).willReturn(localTransaction);
    CciLocalTransactionManager tm = new CciLocalTransactionManager();
    tm.setConnectionFactory(connectionFactory);
    TransactionTemplate tt = new TransactionTemplate(tm);
    try {
        tt.execute(new TransactionCallback<Void>() {

            @Override
            public Void doInTransaction(TransactionStatus status) {
                assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(connectionFactory));
                CciTemplate ct = new CciTemplate(connectionFactory);
                ct.execute(interactionSpec, record, record);
                throw new DataRetrievalFailureException("error");
            }
        });
    } catch (Exception ex) {
    }
    verify(localTransaction).begin();
    verify(interaction).close();
    verify(localTransaction).rollback();
    verify(connection).close();
}
Also used : LocalTransaction(javax.resource.cci.LocalTransaction) Interaction(javax.resource.cci.Interaction) InteractionSpec(javax.resource.cci.InteractionSpec) Connection(javax.resource.cci.Connection) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) CciLocalTransactionManager(org.springframework.jca.cci.connection.CciLocalTransactionManager) CciTemplate(org.springframework.jca.cci.core.CciTemplate) ResourceException(javax.resource.ResourceException) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) ConnectionFactory(javax.resource.cci.ConnectionFactory) Record(javax.resource.cci.Record) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) Test(org.junit.Test)

Example 62 with TransactionStatus

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

the class WebSphereUowTransactionManagerTests method newTransactionWithRollbackOnly.

@Test
public void newTransactionWithRollbackOnly() {
    MockUOWManager manager = new MockUOWManager();
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    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());
            status.setRollbackOnly();
            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());
    assertTrue(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 63 with TransactionStatus

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

the class WebSphereUowTransactionManagerTests method propagationNeverFailsInCaseOfExistingTransaction.

@Test
public void propagationNeverFailsInCaseOfExistingTransaction() {
    MockUOWManager manager = new MockUOWManager();
    manager.setUOWStatus(UOWManager.UOW_STATUS_ACTIVE);
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_NEVER);
    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 64 with TransactionStatus

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

the class WebSphereUowTransactionManagerTests method doTestExistingTransactionWithParticipation.

private void doTestExistingTransactionWithParticipation(int propagationBehavior) {
    MockUOWManager manager = new MockUOWManager();
    final WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    final DefaultTransactionDefinition definition2 = new DefaultTransactionDefinition();
    definition2.setPropagationBehavior(propagationBehavior);
    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());
                    assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
                    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
                    return "result2";
                }
            }));
            return "result";
        }
    }));
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    assertEquals(0, manager.getUOWTimeout());
    assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
    assertTrue(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 65 with TransactionStatus

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

the class WebSphereUowTransactionManagerTests method newTransactionWithRollback.

@Test
public void newTransactionWithRollback() {
    MockUOWManager manager = new MockUOWManager();
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    try {
        ptm.execute(definition, new TransactionCallback<String>() {

            @Override
            public String doInTransaction(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
                assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
                throw new OptimisticLockingFailureException("");
            }
        });
        fail("Should have thrown OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException ex) {
    // expected
    }
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    assertEquals(0, manager.getUOWTimeout());
    assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
    assertFalse(manager.getJoined());
    assertTrue(manager.getRollbackOnly());
}
Also used : OptimisticLockingFailureException(org.springframework.dao.OptimisticLockingFailureException) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Aggregations

TransactionStatus (org.springframework.transaction.TransactionStatus)359 Test (org.junit.Test)186 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)154 TransactionCallback (org.springframework.transaction.support.TransactionCallback)91 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)72 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)32 List (java.util.List)28 ArrayList (java.util.ArrayList)27 SQLException (java.sql.SQLException)25 Connection (java.sql.Connection)21 Date (java.util.Date)18 PreparedStatement (java.sql.PreparedStatement)17 TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)15 OnmsNode (org.opennms.netmgt.model.OnmsNode)14 ManagerException (com.alibaba.otter.manager.biz.common.exceptions.ManagerException)13 RepeatConfigureException (com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException)13 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)12 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)12 EntityManager (javax.persistence.EntityManager)11 DataSource (javax.sql.DataSource)11