當前位置: 首頁>>代碼示例>>Java>>正文


Java TransactionTemplate.setTransactionManager方法代碼示例

本文整理匯總了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);
        }
    }

}
 
開發者ID:luoyaogui,項目名稱:otter-G,代碼行數:37,代碼來源:AbstractOperationInterceptor.java

示例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();
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:41,代碼來源:JdoTransactionManagerTests.java

示例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;
}
 
開發者ID:HydAu,項目名稱:Camel,代碼行數:10,代碼來源:AbstractJdbcMessageIdRepository.java

示例4: transactionTemplate

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Bean("template")
TransactionTemplate transactionTemplate(){
    TransactionTemplate template = new TransactionTemplate();
    template.setTransactionManager(transactionManager);
    return template;
}
 
開發者ID:PacktPublishing,項目名稱:Spring-5.0-Cookbook,代碼行數:7,代碼來源:SpringContextConfig.java

示例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();
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:42,代碼來源:JdoTransactionManagerTests.java

示例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();
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:43,代碼來源:JdoTransactionManagerTests.java

示例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;
}
 
開發者ID:HydAu,項目名稱:Camel,代碼行數:7,代碼來源:JpaMessageIdRepository.java


注:本文中的org.springframework.transaction.support.TransactionTemplate.setTransactionManager方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。