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;
}
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;
}
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);
}
}
}
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();
}
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());
}
Aggregations