Search in sources :

Example 6 with TransactionCallback

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

the class JpaTransactionManagerTests method testParticipatingTransactionWithRequiresNew.

@Test
public void testParticipatingTransactionWithRequiresNew() {
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    given(factory.createEntityManager()).willReturn(manager);
    given(manager.getTransaction()).willReturn(tx);
    given(manager.isOpen()).willReturn(true);
    final List<String> l = new ArrayList<>();
    l.add("test");
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    Object result = 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;
                }
            });
        }
    });
    assertSame(l, result);
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    verify(manager).flush();
    verify(manager, times(2)).close();
    verify(tx, times(2)).begin();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 7 with TransactionCallback

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

the class JpaTransactionManagerTests method testParticipatingTransactionWithRequiresNewAndPrebound.

@Test
public void testParticipatingTransactionWithRequiresNewAndPrebound() {
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    given(manager.getTransaction()).willReturn(tx);
    final List<String> l = new ArrayList<>();
    l.add("test");
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    TransactionSynchronizationManager.bindResource(factory, new EntityManagerHolder(manager));
    try {
        Object result = tt.execute(new TransactionCallback() {

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

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

Example 8 with TransactionCallback

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

the class JpaTransactionManagerTests method testTransactionRollbackWithAlreadyRolledBack.

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

            @Override
            public Object doInTransaction(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.hasResource(factory));
                EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
                throw new RuntimeException("some exception");
            }
        });
        fail("Should have propagated RuntimeException");
    } catch (RuntimeException ex) {
    // expected
    }
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    verify(manager).close();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 9 with TransactionCallback

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

the class JpaTransactionManagerTests method testParticipatingTransactionWithRollback.

@Test
public void testParticipatingTransactionWithRollback() {
    given(manager.getTransaction()).willReturn(tx);
    given(tx.isActive()).willReturn(true);
    final List<String> l = new ArrayList<>();
    l.add("test");
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    try {
        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);
                        throw new RuntimeException("some exception");
                    }
                });
            }
        });
        fail("Should have propagated RuntimeException");
    } catch (RuntimeException ex) {
    // expected
    }
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    verify(tx).setRollbackOnly();
    verify(tx).rollback();
    verify(manager).close();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 10 with TransactionCallback

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

the class JpaTransactionManagerTests method testTransactionCommitWithPropagationSupports.

@Test
public void testTransactionCommitWithPropagationSupports() {
    given(manager.isOpen()).willReturn(true);
    final List<String> l = new ArrayList<>();
    l.add("test");
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    Object result = tt.execute(new TransactionCallback() {

        @Override
        public Object doInTransaction(TransactionStatus status) {
            assertTrue(!TransactionSynchronizationManager.hasResource(factory));
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            assertTrue(!status.isNewTransaction());
            EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
            return l;
        }
    });
    assertSame(l, result);
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    verify(manager).flush();
    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)101 TransactionStatus (org.springframework.transaction.TransactionStatus)76 Test (org.junit.Test)28 ArrayList (java.util.ArrayList)16 Test (org.junit.jupiter.api.Test)14 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)11 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)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