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();
}
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();
}
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();
}
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();
}
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;
}
Aggregations