本文整理汇总了Java中org.springframework.transaction.support.TransactionTemplate.setTransactionManager方法的典型用法代码示例。如果您正苦于以下问题:Java TransactionTemplate.setTransactionManager方法的具体用法?Java TransactionTemplate.setTransactionManager怎么用?Java TransactionTemplate.setTransactionManager使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.transaction.support.TransactionTemplate
的用法示例。
在下文中一共展示了TransactionTemplate.setTransactionManager方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: init
import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
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);
}
}
}
示例2: testTransactionCommitWithAutoDetectedDataSourceAndNoConnection
import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Test
public void testTransactionCommitWithAutoDetectedDataSourceAndNoConnection() throws SQLException {
final DataSource ds = mock(DataSource.class);
final JdoDialect dialect = mock(JdoDialect.class);
given(pmf.getConnectionFactory()).willReturn(ds);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.currentTransaction()).willReturn(tx);
TransactionTemplate tt = new TransactionTemplate();
given(dialect.getJdbcConnection(pm, false)).willReturn(null);
JdoTransactionManager tm = new JdoTransactionManager();
tm.setPersistenceManagerFactory(pmf);
tm.setJdoDialect(dialect);
tm.afterPropertiesSet();
tt.setTransactionManager(tm);
final List l = new ArrayList();
l.add("test");
assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
Object result = tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
assertTrue("Hasn't thread con", !TransactionSynchronizationManager.hasResource(ds));
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
return l;
}
});
assertTrue("Correct result list", result == l);
assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
assertTrue("Hasn't thread con", !TransactionSynchronizationManager.hasResource(ds));
verify(pm).flush();
verify(pm).close();
verify(dialect).beginTransaction(tx, tt);
verify(dialect).cleanupTransaction(null);
verify(tx).commit();
}
示例3: createTransactionTemplate
import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
/**
* 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;
}
示例4: transactionTemplate
import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Bean("template")
TransactionTemplate transactionTemplate(){
TransactionTemplate template = new TransactionTemplate();
template.setTransactionManager(transactionManager);
return template;
}
示例5: testTransactionCommitWithDataSource
import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Test
public void testTransactionCommitWithDataSource() throws SQLException {
final DataSource ds = mock(DataSource.class);
JdoDialect dialect = mock(JdoDialect.class);
final Connection con = mock(Connection.class);
ConnectionHandle conHandle = new SimpleConnectionHandle(con);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.currentTransaction()).willReturn(tx);
TransactionTemplate tt = new TransactionTemplate();
given(dialect.getJdbcConnection(pm, false)).willReturn(conHandle);
JdoTransactionManager tm = new JdoTransactionManager();
tm.setPersistenceManagerFactory(pmf);
tm.setDataSource(ds);
tm.setJdoDialect(dialect);
tt.setTransactionManager(tm);
final List l = new ArrayList();
l.add("test");
assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
Object result = tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
assertTrue("Has thread con", TransactionSynchronizationManager.hasResource(ds));
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
return l;
}
});
assertTrue("Correct result list", result == l);
assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
assertTrue("Hasn't thread con", !TransactionSynchronizationManager.hasResource(ds));
verify(pm).close();
verify(dialect).beginTransaction(tx, tt);
verify(dialect).releaseJdbcConnection(conHandle, pm);
verify(dialect).cleanupTransaction(null);
verify(tx).commit();
}
示例6: testTransactionCommitWithAutoDetectedDataSource
import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
@Test
public void testTransactionCommitWithAutoDetectedDataSource() throws SQLException {
final DataSource ds = mock(DataSource.class);
JdoDialect dialect = mock(JdoDialect.class);
final Connection con = mock(Connection.class);
ConnectionHandle conHandle = new SimpleConnectionHandle(con);
given(pmf.getConnectionFactory()).willReturn(ds);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.currentTransaction()).willReturn(tx);
TransactionTemplate tt = new TransactionTemplate();
given(dialect.getJdbcConnection(pm, false)).willReturn(conHandle);
JdoTransactionManager tm = new JdoTransactionManager();
tm.setPersistenceManagerFactory(pmf);
tm.setJdoDialect(dialect);
tm.afterPropertiesSet();
tt.setTransactionManager(tm);
final List l = new ArrayList();
l.add("test");
assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
Object result = tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
assertTrue("Has thread con", TransactionSynchronizationManager.hasResource(ds));
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
return l;
}
});
assertTrue("Correct result list", result == l);
assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
assertTrue("Hasn't thread con", !TransactionSynchronizationManager.hasResource(ds));
verify(pm).close();
verify(dialect).beginTransaction(tx, tt);
verify(dialect).releaseJdbcConnection(conHandle, pm);
verify(dialect).cleanupTransaction(null);
verify(tx).commit();
}
示例7: createTransactionTemplate
import org.springframework.transaction.support.TransactionTemplate; //导入方法依赖的package包/类
private static TransactionTemplate createTransactionTemplate(EntityManagerFactory entityManagerFactory) {
TransactionTemplate transactionTemplate = new TransactionTemplate();
transactionTemplate.setTransactionManager(new JpaTransactionManager(entityManagerFactory));
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
return transactionTemplate;
}