Search in sources :

Example 46 with PlatformTransactionManager

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);
}
Also used : ResourceStorageDao(org.opennms.netmgt.dao.api.ResourceStorageDao) HashMap(java.util.HashMap) Scheduler(org.opennms.netmgt.scheduler.Scheduler) CollectionSpecification(org.opennms.netmgt.collection.core.CollectionSpecification) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) IpInterfaceDao(org.opennms.netmgt.dao.api.IpInterfaceDao) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) RrdPersisterFactory(org.opennms.netmgt.collection.persistence.rrd.RrdPersisterFactory) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) SchedulingCompletedFlag(org.opennms.netmgt.collectd.Collectd.SchedulingCompletedFlag)

Example 47 with PlatformTransactionManager

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();
}
Also used : MockTransactionTemplate(org.opennms.netmgt.dao.mock.MockTransactionTemplate) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus) SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 48 with PlatformTransactionManager

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);
    }
}
Also used : UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionException(org.springframework.transaction.TransactionException) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) HeuristicCompletionException(org.springframework.transaction.HeuristicCompletionException) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 49 with PlatformTransactionManager

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);
    }
}
Also used : HeuristicCompletionException(org.springframework.transaction.HeuristicCompletionException) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionException(org.springframework.transaction.TransactionException) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) HeuristicCompletionException(org.springframework.transaction.HeuristicCompletionException) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 50 with PlatformTransactionManager

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;
}
Also used : CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) Map(java.util.Map) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) TransactionException(org.springframework.transaction.TransactionException) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) HeuristicCompletionException(org.springframework.transaction.HeuristicCompletionException)

Aggregations

PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)74 Test (org.junit.jupiter.api.Test)24 TransactionStatus (org.springframework.transaction.TransactionStatus)15 Test (org.junit.Test)14 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)10 TestBean (org.springframework.beans.testfixture.beans.TestBean)10 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)9 BeanFactory (org.springframework.beans.factory.BeanFactory)8 Method (java.lang.reflect.Method)7 CannotCreateTransactionException (org.springframework.transaction.CannotCreateTransactionException)7 TransactionException (org.springframework.transaction.TransactionException)7 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)7 StreamCapableTransactionalOperationAdapter (org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter)6 HeuristicCompletionException (org.springframework.transaction.HeuristicCompletionException)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 DataSource (javax.sql.DataSource)5 ServiceException (org.broadleafcommerce.common.exception.ServiceException)5 PersistenceResponse (org.broadleafcommerce.openadmin.server.service.persistence.PersistenceResponse)5 IpInterfaceDao (org.opennms.netmgt.dao.api.IpInterfaceDao)5