Search in sources :

Example 1 with TransactionTemplate

use of org.springframework.transaction.support.TransactionTemplate in project cas by apereo.

the class CasSupportJdbcAuditConfiguration method inspektrAuditTransactionTemplate.

@Bean
public TransactionTemplate inspektrAuditTransactionTemplate() {
    final TransactionTemplate t = new TransactionTemplate(inspektrAuditTransactionManager());
    t.setIsolationLevelName(casProperties.getAudit().getJdbc().getIsolationLevelName());
    t.setPropagationBehaviorName(casProperties.getAudit().getJdbc().getPropagationBehaviorName());
    return t;
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Bean(org.springframework.context.annotation.Bean) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Example 2 with TransactionTemplate

use of org.springframework.transaction.support.TransactionTemplate in project cas by apereo.

the class JpaTicketRegistryTests method grantServiceTicketInTransaction.

private ServiceTicket grantServiceTicketInTransaction(final TicketGrantingTicket parent) {
    return new TransactionTemplate(txManager).execute(status -> {
        final ServiceTicket st = newST(parent);
        ticketRegistry.addTicket(st);
        return st;
    });
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) ServiceTicket(org.apereo.cas.ticket.ServiceTicket)

Example 3 with TransactionTemplate

use of org.springframework.transaction.support.TransactionTemplate in project cas by apereo.

the class JpaTicketRegistryTests method grantProxyGrantingTicketInTransaction.

private ProxyGrantingTicket grantProxyGrantingTicketInTransaction(final ServiceTicket parent) {
    return new TransactionTemplate(txManager).execute(status -> {
        final ProxyGrantingTicket pgt = newPGT(parent);
        ticketRegistry.addTicket(pgt);
        return pgt;
    });
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) ProxyGrantingTicket(org.apereo.cas.ticket.proxy.ProxyGrantingTicket)

Example 4 with TransactionTemplate

use of org.springframework.transaction.support.TransactionTemplate 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)

Example 5 with TransactionTemplate

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

the class PersistenceContextTransactionTests method setUp.

@Before
public void setUp() throws Exception {
    factory = mock(EntityManagerFactory.class);
    manager = mock(EntityManager.class);
    tx = mock(EntityTransaction.class);
    JpaTransactionManager tm = new JpaTransactionManager(factory);
    tt = new TransactionTemplate(tm);
    given(factory.createEntityManager()).willReturn(manager);
    given(manager.getTransaction()).willReturn(tx);
    given(manager.isOpen()).willReturn(true);
    bean = new EntityManagerHoldingBean();
    @SuppressWarnings("serial") PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor() {

        @Override
        protected EntityManagerFactory findEntityManagerFactory(String unitName, String requestingBeanName) {
            return factory;
        }
    };
    pabpp.postProcessPropertyValues(null, null, bean, "bean");
    assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) JpaTransactionManager(org.springframework.orm.jpa.JpaTransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Before(org.junit.Before)

Aggregations

TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)287 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)178 TransactionStatus (org.springframework.transaction.TransactionStatus)158 Test (org.junit.jupiter.api.Test)139 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)70 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)56 UserTransaction (jakarta.transaction.UserTransaction)43 SQLException (java.sql.SQLException)36 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)29 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)25 Connection (java.sql.Connection)23 Test (org.junit.Test)22 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)22 InOrder (org.mockito.InOrder)21 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)18 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)17 DataSource (javax.sql.DataSource)16 DatabaseMetaData (java.sql.DatabaseMetaData)12 PreparedStatement (java.sql.PreparedStatement)12 Savepoint (java.sql.Savepoint)12