use of org.springframework.transaction.PlatformTransactionManager in project opennms by OpenNMS.
the class CollectableServiceTest method createCollectableService.
private void createCollectableService() throws CollectionInitializationException, IOException {
// Mock it all!
OnmsIpInterface iface = mock(OnmsIpInterface.class, RETURNS_DEEP_STUBS);
IpInterfaceDao ifaceDao = mock(IpInterfaceDao.class);
spec = mock(CollectionSpecification.class);
scheduler = mock(Scheduler.class);
SchedulingCompletedFlag schedulingCompletedFlag = mock(SchedulingCompletedFlag.class);
PlatformTransactionManager transMgr = mock(PlatformTransactionManager.class);
RrdPersisterFactory persisterFactory = new RrdPersisterFactory();
persisterFactory.setRrdStrategy(rrdStrategy);
ResourceStorageDao resourceStorageDao = mock(ResourceStorageDao.class);
// Disable thresholding
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("thresholding-enabled", Boolean.FALSE.toString());
ServiceParameters params = new ServiceParameters(paramsMap);
when(iface.getNode().getId()).thenReturn(1);
when(spec.getServiceParameters()).thenReturn(params);
when(spec.getRrdRepository(any())).thenReturn(createRrdRepository());
when(ifaceDao.load(any())).thenReturn(iface);
when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress());
service = new CollectableService(iface, ifaceDao, spec, scheduler, schedulingCompletedFlag, transMgr, persisterFactory, resourceStorageDao);
}
use of org.springframework.transaction.PlatformTransactionManager in project opennms by OpenNMS.
the class CollectdTest method setupTransactionManager.
/**
* Add a dummy transaction manager that has mock calls to commit() and rollback()
*/
private void setupTransactionManager() {
PlatformTransactionManager m_transactionManager = m_easyMockUtils.createMock(PlatformTransactionManager.class);
TransactionTemplate transactionTemplate = new TransactionTemplate(m_transactionManager);
m_collectd.setTransactionTemplate(transactionTemplate);
expect(m_transactionManager.getTransaction(isA(TransactionDefinition.class))).andReturn(new SimpleTransactionStatus()).anyTimes();
m_transactionManager.rollback(isA(TransactionStatus.class));
expectLastCall().anyTimes();
m_transactionManager.commit(isA(TransactionStatus.class));
expectLastCall().anyTimes();
}
use of org.springframework.transaction.PlatformTransactionManager in project spring-data-commons by spring-projects.
the class ChainedTransactionManager method rollback.
/*
* (non-Javadoc)
* @see org.springframework.transaction.PlatformTransactionManager#rollback(org.springframework.transaction.TransactionStatus)
*/
public void rollback(TransactionStatus status) throws TransactionException {
Exception rollbackException = null;
PlatformTransactionManager rollbackExceptionTransactionManager = null;
MultiTransactionStatus multiTransactionStatus = (MultiTransactionStatus) status;
for (PlatformTransactionManager transactionManager : reverse(transactionManagers)) {
try {
multiTransactionStatus.rollback(transactionManager);
} catch (Exception ex) {
if (rollbackException == null) {
rollbackException = ex;
rollbackExceptionTransactionManager = transactionManager;
} else {
LOGGER.warn("Rollback exception (" + transactionManager + ") " + ex.getMessage(), ex);
}
}
}
if (multiTransactionStatus.isNewSynchonization()) {
synchronizationManager.clearSynchronization();
}
if (rollbackException != null) {
throw new UnexpectedRollbackException("Rollback exception, originated at (" + rollbackExceptionTransactionManager + ") " + rollbackException.getMessage(), rollbackException);
}
}
use of org.springframework.transaction.PlatformTransactionManager in project spring-data-commons by spring-projects.
the class ChainedTransactionManager method commit.
/*
* (non-Javadoc)
* @see org.springframework.transaction.PlatformTransactionManager#commit(org.springframework.transaction.TransactionStatus)
*/
public void commit(TransactionStatus status) throws TransactionException {
MultiTransactionStatus multiTransactionStatus = (MultiTransactionStatus) status;
boolean commit = true;
Exception commitException = null;
PlatformTransactionManager commitExceptionTransactionManager = null;
for (PlatformTransactionManager transactionManager : reverse(transactionManagers)) {
if (commit) {
try {
multiTransactionStatus.commit(transactionManager);
} catch (Exception ex) {
commit = false;
commitException = ex;
commitExceptionTransactionManager = transactionManager;
}
} else {
try {
multiTransactionStatus.rollback(transactionManager);
} catch (Exception ex) {
LOGGER.warn("Rollback exception (after commit) (" + transactionManager + ") " + ex.getMessage(), ex);
}
}
}
if (multiTransactionStatus.isNewSynchonization()) {
synchronizationManager.clearSynchronization();
}
if (commitException != null) {
boolean firstTransactionManagerFailed = commitExceptionTransactionManager == getLastTransactionManager();
int transactionState = firstTransactionManagerFailed ? HeuristicCompletionException.STATE_ROLLED_BACK : HeuristicCompletionException.STATE_MIXED;
throw new HeuristicCompletionException(transactionState, commitException);
}
}
use of org.springframework.transaction.PlatformTransactionManager in project spring-data-commons by spring-projects.
the class ChainedTransactionManager method getTransaction.
/*
* (non-Javadoc)
* @see org.springframework.transaction.PlatformTransactionManager#getTransaction(org.springframework.transaction.TransactionDefinition)
*/
public MultiTransactionStatus getTransaction(@Nullable TransactionDefinition definition) throws TransactionException {
MultiTransactionStatus mts = new MultiTransactionStatus(transactionManagers.get(0));
if (definition == null) {
return mts;
}
if (!synchronizationManager.isSynchronizationActive()) {
synchronizationManager.initSynchronization();
mts.setNewSynchonization();
}
try {
for (PlatformTransactionManager transactionManager : transactionManagers) {
mts.registerTransactionManager(definition, transactionManager);
}
} catch (Exception ex) {
Map<PlatformTransactionManager, TransactionStatus> transactionStatuses = mts.getTransactionStatuses();
for (PlatformTransactionManager transactionManager : transactionManagers) {
try {
if (transactionStatuses.get(transactionManager) != null) {
transactionManager.rollback(transactionStatuses.get(transactionManager));
}
} catch (Exception ex2) {
LOGGER.warn("Rollback exception (" + transactionManager + ") " + ex2.getMessage(), ex2);
}
}
if (mts.isNewSynchonization()) {
synchronizationManager.clearSynchronization();
}
throw new CannotCreateTransactionException(ex.getMessage(), ex);
}
return mts;
}
Aggregations