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


Java TransactionTemplate.setPropagationBehavior方法代碼示例

本文整理匯總了Java中org.springframework.transaction.support.TransactionTemplate.setPropagationBehavior方法的典型用法代碼示例。如果您正苦於以下問題:Java TransactionTemplate.setPropagationBehavior方法的具體用法?Java TransactionTemplate.setPropagationBehavior怎麽用?Java TransactionTemplate.setPropagationBehavior使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.transaction.support.TransactionTemplate的用法示例。


在下文中一共展示了TransactionTemplate.setPropagationBehavior方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: jtaTransactionManagerWithExistingAndPropagationSupports

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void jtaTransactionManagerWithExistingAndPropagationSupports() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

	final TransactionSynchronization synch = mock(TransactionSynchronization.class);

	JtaTransactionManager ptm = newJtaTransactionManager(ut);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
			TransactionSynchronizationManager.registerSynchronization(synch);
			status.setRollbackOnly();
		}
	});
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

	verify(ut).setRollbackOnly();
	verify(synch).beforeCompletion();
	verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:26,代碼來源:JtaTransactionManagerTests.java

示例2: testTransactionWithPropagationNestedAndRollback

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void testTransactionWithPropagationNestedAndRollback() throws Exception {
	final TransactionTemplate tt = new TransactionTemplate(tm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
	assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());

	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
			assertTrue("Is new transaction", status.isNewTransaction());
			status.setRollbackOnly();
		}
	});

	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
	verify(con).rollback();
	verify(con).close();
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:20,代碼來源:DataSourceTransactionManagerTests.java

示例3: jtaTransactionManagerWithPropagationRequiresNewAndExisting

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExisting() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	TransactionManager tm = mock(TransactionManager.class);
	Transaction tx = mock(Transaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
	given(tm.suspend()).willReturn(tx);

	JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
		}
	});
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

	verify(ut).begin();
	verify(ut).commit();
	verify(tm).resume(tx);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:25,代碼來源:JtaTransactionManagerTests.java

示例4: execute

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Override
public <T> T execute(final CommandConfig config, final Command<T> command) {
    LOGGER.debug("Running command with propagation {}", config.getTransactionPropagation());

    TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
    transactionTemplate.setPropagationBehavior(getPropagation(config));

    T result = transactionTemplate.execute(new TransactionCallback<T>() {
        @Override
        public T doInTransaction(TransactionStatus status) {
            return next.execute(config, command);
        }
    });

    return result;
}
 
開發者ID:flowable,項目名稱:flowable-engine,代碼行數:17,代碼來源:SpringTransactionInterceptor.java

示例5: testTransactionWithPropagationNested

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void testTransactionWithPropagationNested() throws Exception {
	final TransactionTemplate tt = new TransactionTemplate(tm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
	assertTrue("Synchronization not active", !TransactionSynchronizationManager.isSynchronizationActive());

	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
			assertTrue("Is new transaction", status.isNewTransaction());
		}
	});

	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
	verify(con).commit();
	verify(con).close();
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:19,代碼來源:DataSourceTransactionManagerTests.java

示例6: jtaTransactionManagerWithPropagationRequiresNewAndSuspensionNotSupported

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndSuspensionNotSupported() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

	JtaTransactionManager ptm = newJtaTransactionManager(ut);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
	try {
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
			}
		});
		fail("Should have thrown TransactionSuspensionNotSupportedException");
	}
	catch (TransactionSuspensionNotSupportedException ex) {
		// expected
	}
	assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:23,代碼來源:JtaTransactionManagerTests.java

示例7: jtaTransactionManagerWithNestedBegin

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void jtaTransactionManagerWithNestedBegin() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

	JtaTransactionManager ptm = newJtaTransactionManager(ut);
	TransactionTemplate tt = new TransactionTemplate(ptm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) {
			// something transactional
		}
	});

	verify(ut).begin();
	verify(ut).commit();
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:19,代碼來源:JtaTransactionManagerTests.java

示例8: jtaTransactionManagerWithNotSupportedExceptionOnNestedBegin

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void jtaTransactionManagerWithNotSupportedExceptionOnNestedBegin() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
	willThrow(new NotSupportedException("not supported")).given(ut).begin();

	try {
		JtaTransactionManager ptm = newJtaTransactionManager(ut);
		TransactionTemplate tt = new TransactionTemplate(ptm);
		tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				// something transactional
			}
		});
		fail("Should have thrown NestedTransactionNotSupportedException");
	}
	catch (NestedTransactionNotSupportedException ex) {
		// expected
	}
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:23,代碼來源:JtaTransactionManagerTests.java

示例9: jtaTransactionManagerWithUnsupportedOperationExceptionOnNestedBegin

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void jtaTransactionManagerWithUnsupportedOperationExceptionOnNestedBegin() throws Exception {
	UserTransaction ut = mock(UserTransaction.class);
	given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
	willThrow(new UnsupportedOperationException("not supported")).given(ut).begin();

	try {
		JtaTransactionManager ptm = newJtaTransactionManager(ut);
		TransactionTemplate tt = new TransactionTemplate(ptm);
		tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				// something transactional
			}
		});
		fail("Should have thrown NestedTransactionNotSupportedException");
	}
	catch (NestedTransactionNotSupportedException ex) {
		// expected
	}
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:23,代碼來源:JtaTransactionManagerTests.java

示例10: testTransactionWithPropagationSupports

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void testTransactionWithPropagationSupports() throws Exception {
	TransactionTemplate tt = new TransactionTemplate(tm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));

	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
			assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
			assertTrue("Is not new transaction", !status.isNewTransaction());
			assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
			assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
		}
	});

	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:19,代碼來源:DataSourceTransactionManagerTests.java

示例11: testTransactionWithPropagationNever

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void testTransactionWithPropagationNever() throws Exception {
	TransactionTemplate tt = new TransactionTemplate(tm);
	tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NEVER);
	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));

	tt.execute(new TransactionCallbackWithoutResult() {
		@Override
		protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
			assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
			assertTrue("Is not new transaction", !status.isNewTransaction());
		}
	});

	assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:17,代碼來源:DataSourceTransactionManagerTests.java

示例12: 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

示例13: put

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
/**
 * puts the block into the database.
 *
 * @param blocks
 *            the blocks to use.
 */
@Override
public void put(final Block... blocks) {
	synchronized (this) {
		if (closed) {
			return;
		}
	}
	if (LOG.isInfoEnabled()) {
		LOG.info("STARTED put, {} blocks", NumberFormat.getIntegerInstance().format(blocks.length));
	}
	final DataSourceTransactionManager tsMan = new DataSourceTransactionManager(ds);

	final TransactionTemplate txTemplate = new TransactionTemplate(tsMan);
	// set behavior
	txTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
	try {
		txTemplate.execute(new TransactionCallback(blocks));
	} catch (final DataAccessException e) {
		if (LOG.isErrorEnabled()) {
			LOG.error("data access exception", e);
		}
	}
	if (LOG.isInfoEnabled()) {
		LOG.info("SUCCESS put, {} blocks", NumberFormat.getIntegerInstance().format(blocks.length));
	}
}
 
開發者ID:coranos,項目名稱:neo-java,代碼行數:33,代碼來源:BlockDbH2Impl.java

示例14: executeCommand

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
public <T> T executeCommand(final Command<T> command) {
	TransactionTemplate template = new TransactionTemplate(platformTransactionManager);
    template.setPropagationBehavior(springPropagationBehaviour);
    return template.execute(new TransactionCallback<T>() {
		public T doInTransaction(TransactionStatus status) {
			return command.execute(context);
		}
    	
	});
}
 
開發者ID:youseries,項目名稱:uflo,代碼行數:11,代碼來源:SpringTransactionCommandService.java

示例15: executeCommandInNewTransaction

import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
public <T> T executeCommandInNewTransaction(final Command<T> command) {
	TransactionTemplate template = new TransactionTemplate(platformTransactionManager);
	template.setPropagationBehavior(newSpringPropagationBehaviour);
	return template.execute(new TransactionCallback<T>() {
		public T doInTransaction(TransactionStatus status) {
			return command.execute(context);
		}
		
	});
}
 
開發者ID:youseries,項目名稱:uflo,代碼行數:11,代碼來源:SpringTransactionCommandService.java


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