Search in sources :

Example 56 with TransactionCallback

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

the class JpaTransactionManagerTests method testTransactionRollbackWithPreboundAndPropagationSupports.

@Test
public void testTransactionRollbackWithPreboundAndPropagationSupports() {
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    TransactionSynchronizationManager.bindResource(factory, new EntityManagerHolder(manager));
    try {
        tt.execute(new TransactionCallback() {

            @Override
            public Object doInTransaction(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.hasResource(factory));
                assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                assertTrue(!status.isNewTransaction());
                EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
                status.setRollbackOnly();
                return null;
            }
        });
        assertTrue(TransactionSynchronizationManager.hasResource(factory));
        assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    } finally {
        TransactionSynchronizationManager.unbindResource(factory);
    }
    verify(manager).flush();
    verify(manager).clear();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 57 with TransactionCallback

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

the class JpaTransactionManagerTests method testTransactionRollback.

@Test
public void testTransactionRollback() {
    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));
                EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
                throw new RuntimeException("some exception");
            }
        });
        fail("Should have propagated RuntimeException");
    } catch (RuntimeException ex) {
        // expected
        assertEquals("some exception", ex.getMessage());
    }
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    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 58 with TransactionCallback

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

the class JpaTransactionManagerTests method testTransactionCommitWithPreboundAndPropagationSupports.

@Test
public void testTransactionCommitWithPreboundAndPropagationSupports() {
    final List<String> l = new ArrayList<>();
    l.add("test");
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    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) {
                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());
    } finally {
        TransactionSynchronizationManager.unbindResource(factory);
    }
    verify(manager).flush();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 59 with TransactionCallback

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

the class JpaTransactionManagerTests method testTransactionRollbackWithPrebound.

@Test
public void testTransactionRollbackWithPrebound() {
    given(manager.getTransaction()).willReturn(tx);
    given(tx.isActive()).willReturn(true);
    assertTrue(!TransactionSynchronizationManager.hasResource(factory));
    assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    TransactionSynchronizationManager.bindResource(factory, new EntityManagerHolder(manager));
    try {
        tt.execute(new TransactionCallback() {

            @Override
            public Object doInTransaction(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.hasResource(factory));
                assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
                status.setRollbackOnly();
                return null;
            }
        });
        assertTrue(TransactionSynchronizationManager.hasResource(factory));
        assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
    } finally {
        TransactionSynchronizationManager.unbindResource(factory);
    }
    verify(tx).begin();
    verify(tx).rollback();
    verify(manager).clear();
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 60 with TransactionCallback

use of org.springframework.transaction.support.TransactionCallback in project bamboobsc by billchen198318.

the class MeasureDataCalendarUtils method findMeasureData.

/**
	 * 這個 method 主要給 Expression 使用, 避免近入LogicService 處理時影響到 Transaction
	 * 
	 * @param kpiId
	 * @param date
	 * @param frequency
	 * @param orgaId
	 * @param emplId
	 * @return
	 * @throws ServiceException
	 * @throws Exception
	 */
@SuppressWarnings("rawtypes")
public static MeasureDataVO findMeasureData(String kpiId, String date, String frequency, String orgaId, String emplId) throws ServiceException, Exception {
    transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_DEFAULT);
    transactionTemplate.setReadOnly(true);
    MeasureDataVO measureData = null;
    try {
        measureData = (MeasureDataVO) transactionTemplate.execute(new TransactionCallback() {

            @Override
            public Object doInTransaction(TransactionStatus status) {
                MeasureDataVO value = new MeasureDataVO();
                value.setKpiId(kpiId);
                value.setDate(date);
                value.setFrequency(frequency);
                value.setOrgId(orgaId);
                value.setEmpId(emplId);
                try {
                    DefaultResult<MeasureDataVO> mdResult = measureDataService.findByUK(value);
                    if (mdResult.getValue() != null) {
                        value = mdResult.getValue();
                    } else {
                        value = null;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    value = null;
                }
                return value;
            }
        });
    } catch (Exception e) {
        throw e;
    }
    return measureData;
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) TransactionStatus(org.springframework.transaction.TransactionStatus) MeasureDataVO(com.netsteadfast.greenstep.vo.MeasureDataVO) DefaultResult(com.netsteadfast.greenstep.base.model.DefaultResult) ServiceException(com.netsteadfast.greenstep.base.exception.ServiceException)

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