Search in sources :

Example 1 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project opennms by OpenNMS.

the class TransactionAwareEventForwarderIT method testTwoTransactions.

@Test
public void testTwoTransactions() {
    m_transTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
            Event event = sendEvent();
            getEventAnticipator().anticipateEvent(event);
        }
    });
    m_transTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
            Event event = sendEvent();
            getEventAnticipator().anticipateEvent(event);
        }
    });
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) Event(org.opennms.netmgt.xml.event.Event) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Example 2 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project opennms by OpenNMS.

the class NodeCacheImpl method getNodeAndCategoryInfo.

private Map<String, String> getNodeAndCategoryInfo(Long nodeId) {
    final Map<String, String> result = new HashMap<>();
    // safety check
    if (nodeId != null) {
        LOG.debug("Fetching node data from database into cache");
        // wrap in a transaction so that Hibernate session is bound and getCategories works
        transactionOperations.execute(new TransactionCallbackWithoutResult() {

            @Override
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                OnmsNode node = nodeDao.get(nodeId.toString());
                if (node != null) {
                    populateBodyWithNodeInfo(result, node);
                }
            }
        });
    }
    return result;
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) HashMap(java.util.HashMap) TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 3 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project opennms by OpenNMS.

the class AlarmLifecycleEventsIT method canGenerateAlarmDeletedLifecycleEvents.

@Test
public void canGenerateAlarmDeletedLifecycleEvents() {
    // Expect an alarmCreated event
    m_eventMgr.getEventAnticipator().resetAnticipated();
    m_eventMgr.getEventAnticipator().anticipateEvent(new EventBuilder(EventConstants.ALARM_CREATED_UEI, "alarmd").getEvent());
    m_eventMgr.getEventAnticipator().setDiscardUnanticipated(true);
    // Send a nodeDown
    sendNodeDownEvent(1);
    // Wait until we've received the alarmCreated event
    await().until(allAnticipatedEventsWereReceived());
    // Expect an alarmCreated and a alarmCleared event
    m_eventMgr.getEventAnticipator().resetAnticipated();
    m_eventMgr.getEventAnticipator().anticipateEvent(new EventBuilder(EventConstants.ALARM_CREATED_UEI, "alarmd").getEvent());
    m_eventMgr.getEventAnticipator().anticipateEvent(new EventBuilder(EventConstants.ALARM_CLEARED_UEI, "alarmd").getEvent());
    m_eventMgr.getEventAnticipator().setDiscardUnanticipated(true);
    // Send a nodeUp
    sendNodeUpEvent(1);
    // Wait until we've received the alarmCreated and alarmCleared events
    // We need to wait for the cosmicClear automation, which currently runs every 30 seconds
    await().atMost(1, MINUTES).until(allAnticipatedEventsWereReceived());
    // Expect an alarmDeleted event
    m_eventMgr.getEventAnticipator().anticipateEvent(new EventBuilder(EventConstants.ALARM_DELETED_EVENT_UEI, "alarmd").getEvent());
    m_eventMgr.getEventAnticipator().setDiscardUnanticipated(true);
    // We need to wait for the cleanUp automation, which currently runs every 60 seconds
    // but it will only trigger then 'lastautomationtime' and 'lasteventtime' < "5 minutes ago"
    // so we cheat a little and update the timestamps ourselves instead of waiting
    transactionTemplate.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            Criteria criteria = new Criteria(OnmsAlarm.class);
            criteria.addRestriction(new EqRestriction("node.id", 1));
            criteria.addRestriction(new EqRestriction("uei", EventConstants.NODE_DOWN_EVENT_UEI));
            for (OnmsAlarm alarm : m_alarmDao.findMatching(criteria)) {
                LocalDateTime tenMinutesAgo = LocalDateTime.now().minusMinutes(10);
                Date then = Date.from(tenMinutesAgo.toInstant(ZoneOffset.UTC));
                alarm.setLastAutomationTime(then);
                alarm.setLastEventTime(then);
                m_alarmDao.save(alarm);
            }
            m_alarmDao.flush();
        }
    });
    // Wait until we've received the alarmDeleted event
    await().atMost(2, MINUTES).until(allAnticipatedEventsWereReceived());
}
Also used : LocalDateTime(java.time.LocalDateTime) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) EqRestriction(org.opennms.core.criteria.restrictions.EqRestriction) TransactionStatus(org.springframework.transaction.TransactionStatus) Criteria(org.opennms.core.criteria.Criteria) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Date(java.util.Date) Test(org.junit.Test)

Example 4 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project opennms by OpenNMS.

the class AlarmRestServiceIT method afterServletStart.

@Override
protected void afterServletStart() {
    MockLogAppender.setupLogging(true, "DEBUG");
    m_template.execute(new TransactionCallbackWithoutResult() {

        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            m_databasePopulator.populateDatabase();
        }
    });
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Example 5 with TransactionCallbackWithoutResult

use of org.springframework.transaction.support.TransactionCallbackWithoutResult in project opennms by OpenNMS.

the class InsSession method getEventsByCriteria.

private void getEventsByCriteria() {
    LOG.debug("clearing events");
    clearEvents();
    final BeanFactoryReference bf = BeanUtils.getBeanFactory("daoContext");
    final EventDao eventDao = BeanUtils.getBean(bf, "eventDao", EventDao.class);
    final TransactionTemplate transTemplate = BeanUtils.getBean(bf, "transactionTemplate", TransactionTemplate.class);
    try {
        transTemplate.execute(new TransactionCallbackWithoutResult() {

            @Override
            public void doInTransactionWithoutResult(final TransactionStatus status) {
                LOG.debug("Entering transaction call back: selection with criteria: {}", criteriaRestriction);
                final OnmsCriteria criteria = new OnmsCriteria(OnmsEvent.class);
                criteria.add(Restrictions.sqlRestriction(criteriaRestriction));
                final List<OnmsEvent> events = eventDao.findMatching(criteria);
                LOG.info("Found {} event(s) with criteria: {}", events.size(), criteriaRestriction);
                for (final OnmsEvent onmsEvent : events) {
                    final Event xmlEvent = getXMLEvent(onmsEvent);
                    if (xmlEvent != null)
                        addEvent(xmlEvent);
                }
            }
        });
    } catch (final RuntimeException e) {
        LOG.error("Error while getting events.", e);
    }
}
Also used : BeanFactoryReference(org.springframework.beans.factory.access.BeanFactoryReference) OnmsEvent(org.opennms.netmgt.model.OnmsEvent) EventDao(org.opennms.netmgt.dao.api.EventDao) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) OnmsEvent(org.opennms.netmgt.model.OnmsEvent) Event(org.opennms.netmgt.xml.event.Event) OnmsCriteria(org.opennms.netmgt.model.OnmsCriteria) ArrayList(java.util.ArrayList) List(java.util.List) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult)

Aggregations

TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)328 TransactionStatus (org.springframework.transaction.TransactionStatus)277 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)179 Test (org.junit.jupiter.api.Test)158 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)70 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)54 UserTransaction (jakarta.transaction.UserTransaction)43 SQLException (java.sql.SQLException)37 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)29 Test (org.junit.Test)25 Connection (java.sql.Connection)23 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)22 InOrder (org.mockito.InOrder)21 Date (java.util.Date)18 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)18 List (java.util.List)16 DataSource (javax.sql.DataSource)15 ArrayList (java.util.ArrayList)14 ManagerException (com.alibaba.otter.manager.biz.common.exceptions.ManagerException)13 RepeatConfigureException (com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException)13