Search in sources :

Example 51 with TransactionCallback

use of org.springframework.transaction.support.TransactionCallback 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 52 with TransactionCallback

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

Example 53 with TransactionCallback

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

the class WebSphereUowTransactionManagerTests method uowManagerFoundInJndi.

@Test
public void uowManagerFoundInJndi() {
    MockUOWManager manager = new MockUOWManager();
    ExpectedLookupTemplate jndiTemplate = new ExpectedLookupTemplate(WebSphereUowTransactionManager.DEFAULT_UOW_MANAGER_NAME, manager);
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager();
    ptm.setJndiTemplate(jndiTemplate);
    ptm.afterPropertiesSet();
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    assertEquals("result", ptm.execute(definition, new TransactionCallback<String>() {

        @Override
        public String doInTransaction(TransactionStatus status) {
            return "result";
        }
    }));
    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) ExpectedLookupTemplate(org.springframework.tests.mock.jndi.ExpectedLookupTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 54 with TransactionCallback

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

the class WebSphereUowTransactionManagerTests method newTransactionWithCommitAndTimeout.

@Test
public void newTransactionWithCommitAndTimeout() {
    MockUOWManager manager = new MockUOWManager();
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setTimeout(10);
    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) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
            assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
            return "result";
        }
    }));
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
    assertEquals(10, 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) Test(org.junit.Test)

Example 55 with TransactionCallback

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

the class JpaTransactionManagerTests method testParticipatingTransactionWithCommit.

@Test
public void testParticipatingTransactionWithCommit() {
    given(manager.getTransaction()).willReturn(tx);
    final List<String> l = new ArrayList<>();
    l.add("test");
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallback() {

        @Override
        public Object doInTransaction(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.hasResource(factory));
            return tt.execute(new TransactionCallback() {

                @Override
                public Object doInTransaction(TransactionStatus status) {
                    EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
                    return l;
                }
            });
        }
    });
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    verify(manager).flush();
    verify(tx).commit();
    verify(manager).close();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Aggregations

TransactionCallback (org.springframework.transaction.support.TransactionCallback)100 TransactionStatus (org.springframework.transaction.TransactionStatus)75 Test (org.junit.Test)28 ArrayList (java.util.ArrayList)16 Test (org.junit.jupiter.api.Test)14 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)10 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)10 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)9 PreparedStatement (java.sql.PreparedStatement)8 List (java.util.List)8 GitMaterial (com.thoughtworks.go.config.materials.git.GitMaterial)7 MaterialInstance (com.thoughtworks.go.domain.MaterialInstance)7 Modification (com.thoughtworks.go.domain.materials.Modification)7 PackageMaterialInstance (com.thoughtworks.go.domain.materials.packagematerial.PackageMaterialInstance)7 PluggableSCMMaterialInstance (com.thoughtworks.go.domain.materials.scm.PluggableSCMMaterialInstance)7 Query (org.hibernate.Query)7 BaseDbTest (com.alibaba.otter.node.etl.BaseDbTest)6 DbDialect (com.alibaba.otter.node.etl.common.db.dialect.DbDialect)6 Modifications (com.thoughtworks.go.domain.materials.Modifications)6 HgMaterialInstance (com.thoughtworks.go.domain.materials.mercurial.HgMaterialInstance)6