Search in sources :

Example 6 with DataSourceTransactionManager

use of org.springframework.jdbc.datasource.DataSourceTransactionManager in project Gargoyle by callakrsos.

the class DbUtil method getTransactionedScope.

public static <T> int getTransactionedScope(DataSource dataSource, T userObj, BiTransactionScope<T, NamedParameterJdbcTemplate> consumer, Consumer<Exception> exceptionHandler) {
    //		DataSource dataSource = null;
    try {
        //			dataSource = getDataSource();
        NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
        TransactionTemplate template = new TransactionTemplate();
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
        template.setTransactionManager(transactionManager);
        return template.execute(status -> {
            int result = -1;
            try {
                result = consumer.scope(userObj, namedParameterJdbcTemplate);
            } catch (Exception e) {
                status.setRollbackOnly();
                LOGGER.error(ValueUtil.toString(e));
                if (exceptionHandler != null)
                    exceptionHandler.accept(e);
                result = -1;
            }
            return result;
        });
    } catch (Exception e) {
        exceptionHandler.accept(e);
    } finally {
        try {
            close(dataSource);
        } catch (Exception e) {
        }
    }
    return -1;
}
Also used : NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager) SQLException(java.sql.SQLException) GargoyleException(com.kyj.fx.voeditor.visual.exceptions.GargoyleException) NotSupportException(com.kyj.fx.voeditor.visual.exceptions.NotSupportException)

Example 7 with DataSourceTransactionManager

use of org.springframework.jdbc.datasource.DataSourceTransactionManager in project camel by apache.

the class AbstractJdbcMessageIdRepository method createTransactionTemplate.

/**
     * Creates the transaction template
     */
protected static TransactionTemplate createTransactionTemplate(DataSource dataSource) {
    TransactionTemplate transactionTemplate = new TransactionTemplate();
    transactionTemplate.setTransactionManager(new DataSourceTransactionManager(dataSource));
    transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
    return transactionTemplate;
}
Also used : TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager)

Example 8 with DataSourceTransactionManager

use of org.springframework.jdbc.datasource.DataSourceTransactionManager in project otter by alibaba.

the class AbstractOperationInterceptor method init.

private void init(final JdbcTemplate jdbcTemplate, final String markTableName, final String markTableColumn) {
    int count = jdbcTemplate.queryForInt(MessageFormat.format(checkDataSql, markTableName, GLOBAL_THREAD_COUNT - 1));
    if (count != GLOBAL_THREAD_COUNT) {
        if (logger.isInfoEnabled()) {
            logger.info("Interceptor: init " + markTableName + "'s data.");
        }
        TransactionTemplate transactionTemplate = new TransactionTemplate();
        transactionTemplate.setTransactionManager(new DataSourceTransactionManager(jdbcTemplate.getDataSource()));
        // 注意这里强制使用非事务,保证多线程的可见性
        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
        transactionTemplate.execute(new TransactionCallback() {

            public Object doInTransaction(TransactionStatus status) {
                jdbcTemplate.execute(MessageFormat.format(deleteDataSql, markTableName));
                String batchSql = MessageFormat.format(updateSql, new Object[] { markTableName, markTableColumn });
                jdbcTemplate.batchUpdate(batchSql, new BatchPreparedStatementSetter() {

                    public void setValues(PreparedStatement ps, int idx) throws SQLException {
                        ps.setInt(1, idx);
                        ps.setInt(2, 0);
                    // ps.setNull(3, Types.VARCHAR);
                    }

                    public int getBatchSize() {
                        return GLOBAL_THREAD_COUNT;
                    }
                });
                return null;
            }
        });
        if (logger.isInfoEnabled()) {
            logger.info("Interceptor: Init EROSA Client Data: " + updateSql);
        }
    }
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) BatchPreparedStatementSetter(org.springframework.jdbc.core.BatchPreparedStatementSetter) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) PreparedStatement(java.sql.PreparedStatement) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager)

Example 9 with DataSourceTransactionManager

use of org.springframework.jdbc.datasource.DataSourceTransactionManager in project spring-boot by spring-projects.

the class BatchAutoConfigurationTests method testCustomizeDataSourceTransactionManagerUsingProperties.

@Test
public void testCustomizeDataSourceTransactionManagerUsingProperties() throws Exception {
    this.context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(this.context, "spring.transaction.default-timeout:30", "spring.transaction.rollback-on-commit-failure:true");
    this.context.register(TestConfiguration.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, TransactionAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    this.context.getBean(BatchConfigurer.class);
    DataSourceTransactionManager transactionManager = DataSourceTransactionManager.class.cast(this.context.getBean(BatchConfigurer.class).getTransactionManager());
    assertThat(transactionManager.getDefaultTimeout()).isEqualTo(30);
    assertThat(transactionManager.isRollbackOnCommitFailure()).isTrue();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager) Test(org.junit.Test)

Example 10 with DataSourceTransactionManager

use of org.springframework.jdbc.datasource.DataSourceTransactionManager in project opennms by OpenNMS.

the class OpenNMSITCase method setUp.

@Before
public void setUp() throws Exception {
    MockUtil.println("------------ Begin Test " + this + " --------------------------");
    MockLogAppender.setupLogging();
    if (m_runSupers) {
        createMockNetwork();
        populateDatabase();
        DataSourceFactory.setInstance(m_db);
        SnmpPeerFactory.setInstance(new SnmpPeerFactory(new ByteArrayResource(getSnmpConfig().getBytes())));
        if (isStartEventd()) {
            m_eventdIpcMgr = new EventIpcManagerDefaultImpl(m_registry);
            EventUtilJdbcImpl eventUtil = new EventUtilJdbcImpl();
            AbstractEventUtil.setInstance(eventUtil);
            JdbcEventdServiceManager eventdServiceManager = new JdbcEventdServiceManager();
            eventdServiceManager.setDataSource(m_db);
            eventdServiceManager.afterPropertiesSet();
            /*
                 * Make sure we specify a full resource path since "this" is
                 * the unit test class, which is most likely in another package. 
                 */
            File configFile = ConfigurationTestUtils.getFileForResource(this, "/org/opennms/netmgt/mock/eventconf.xml");
            DefaultEventConfDao eventConfDao = new DefaultEventConfDao();
            eventConfDao.setConfigResource(new FileSystemResource(configFile));
            eventConfDao.afterPropertiesSet();
            EventExpander eventExpander = new EventExpander(m_registry);
            eventExpander.setEventConfDao(eventConfDao);
            eventExpander.setEventUtil(eventUtil);
            eventExpander.afterPropertiesSet();
            EventIpcBroadcastProcessor eventIpcBroadcastProcessor = new EventIpcBroadcastProcessor(m_registry);
            eventIpcBroadcastProcessor.setEventIpcBroadcaster(m_eventdIpcMgr);
            eventIpcBroadcastProcessor.afterPropertiesSet();
            List<EventProcessor> eventProcessors = new ArrayList<EventProcessor>(3);
            eventProcessors.add(eventExpander);
            eventProcessors.add(eventIpcBroadcastProcessor);
            DefaultEventHandlerImpl eventHandler = new DefaultEventHandlerImpl(m_registry);
            eventHandler.setEventProcessors(eventProcessors);
            eventHandler.afterPropertiesSet();
            m_eventdIpcMgr.setHandlerPoolSize(5);
            m_eventdIpcMgr.setEventHandler(eventHandler);
            m_eventdIpcMgr.afterPropertiesSet();
            m_eventProxy = m_eventdIpcMgr;
            EventIpcManagerFactory.setIpcManager(m_eventdIpcMgr);
            EventIpcManagerEventHandlerProxy proxy = new EventIpcManagerEventHandlerProxy();
            proxy.setEventIpcManager(m_eventdIpcMgr);
            proxy.afterPropertiesSet();
            List<EventHandler> eventHandlers = new ArrayList<EventHandler>(1);
            eventHandlers.add(proxy);
            TcpEventReceiver tcpEventReceiver = new TcpEventReceiver();
            tcpEventReceiver.setPort(5837);
            tcpEventReceiver.setEventHandlers(eventHandlers);
            UdpEventReceiver udpEventReceiver = new UdpEventReceiver();
            udpEventReceiver.setPort(5837);
            tcpEventReceiver.setEventHandlers(eventHandlers);
            List<EventReceiver> eventReceivers = new ArrayList<EventReceiver>(2);
            eventReceivers.add(tcpEventReceiver);
            eventReceivers.add(udpEventReceiver);
            m_eventd = new Eventd();
            m_eventd.setEventdServiceManager(eventdServiceManager);
            m_eventd.setEventReceivers(eventReceivers);
            m_eventd.setReceiver(new BroadcastEventProcessor(m_eventdIpcMgr, eventConfDao));
            m_eventd.init();
            m_eventd.start();
        }
    }
    m_transMgr = new DataSourceTransactionManager(DataSourceFactory.getInstance());
}
Also used : EventExpander(org.opennms.netmgt.eventd.EventExpander) EventIpcManagerEventHandlerProxy(org.opennms.netmgt.eventd.adaptors.EventIpcManagerEventHandlerProxy) JdbcEventdServiceManager(org.opennms.netmgt.dao.mock.JdbcEventdServiceManager) Eventd(org.opennms.netmgt.eventd.Eventd) SnmpPeerFactory(org.opennms.netmgt.config.SnmpPeerFactory) ArrayList(java.util.ArrayList) EventHandler(org.opennms.netmgt.eventd.adaptors.EventHandler) ByteArrayResource(org.springframework.core.io.ByteArrayResource) EventIpcManagerDefaultImpl(org.opennms.netmgt.eventd.EventIpcManagerDefaultImpl) FileSystemResource(org.springframework.core.io.FileSystemResource) TcpEventReceiver(org.opennms.netmgt.eventd.adaptors.tcp.TcpEventReceiver) DefaultEventConfDao(org.opennms.netmgt.config.DefaultEventConfDao) DefaultEventHandlerImpl(org.opennms.netmgt.eventd.DefaultEventHandlerImpl) EventReceiver(org.opennms.netmgt.eventd.adaptors.EventReceiver) TcpEventReceiver(org.opennms.netmgt.eventd.adaptors.tcp.TcpEventReceiver) UdpEventReceiver(org.opennms.netmgt.eventd.adaptors.udp.UdpEventReceiver) EventIpcBroadcastProcessor(org.opennms.netmgt.eventd.processor.EventIpcBroadcastProcessor) BroadcastEventProcessor(org.opennms.netmgt.eventd.BroadcastEventProcessor) EventProcessor(org.opennms.netmgt.events.api.EventProcessor) UdpEventReceiver(org.opennms.netmgt.eventd.adaptors.udp.UdpEventReceiver) BroadcastEventProcessor(org.opennms.netmgt.eventd.BroadcastEventProcessor) File(java.io.File) DataSourceTransactionManager(org.springframework.jdbc.datasource.DataSourceTransactionManager) Before(org.junit.Before)

Aggregations

DataSourceTransactionManager (org.springframework.jdbc.datasource.DataSourceTransactionManager)11 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)4 ProcessEngineFactoryBean (org.activiti.spring.ProcessEngineFactoryBean)3 Bean (org.springframework.context.annotation.Bean)3 SQLException (java.sql.SQLException)2 Test (org.junit.Test)2 TransactionStatus (org.springframework.transaction.TransactionStatus)2 GargoyleException (com.kyj.fx.voeditor.visual.exceptions.GargoyleException)1 NotSupportException (com.kyj.fx.voeditor.visual.exceptions.NotSupportException)1 File (java.io.File)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ArrayList (java.util.ArrayList)1 DataSource (javax.sql.DataSource)1 JndiRegistry (org.apache.camel.impl.JndiRegistry)1 SpringTransactionPolicy (org.apache.camel.spring.spi.SpringTransactionPolicy)1 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)1 Before (org.junit.Before)1 DefaultEventConfDao (org.opennms.netmgt.config.DefaultEventConfDao)1 SnmpPeerFactory (org.opennms.netmgt.config.SnmpPeerFactory)1