本文整理匯總了Java中org.springframework.transaction.support.TransactionTemplate.execute方法的典型用法代碼示例。如果您正苦於以下問題:Java TransactionTemplate.execute方法的具體用法?Java TransactionTemplate.execute怎麽用?Java TransactionTemplate.execute使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.transaction.support.TransactionTemplate
的用法示例。
在下文中一共展示了TransactionTemplate.execute方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: updateSql
import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
/**
* 批處理更新操作
*
* @param dbInfoId
* @param sqls
* @return 返回更新的列的數量
* @throws Exception
*/
public int[] updateSql(String dbInfoId, String[] sqls) throws Exception {
final String[] fsqls = this.getFormatArrays(sqls);
if (log.isDebugEnabled()) {
for (String s : fsqls) {
log.debug(s);
}
}
DataSource ds = getDataSourceByDbInfoId(dbInfoId);
final TransactionTemplate transactionTemplate = SpringJdbcUtils.getTransactionTemplate(ds);
return transactionTemplate.execute(new TransactionCallback<int[]>() {
public int[] doInTransaction(TransactionStatus status) {
JdbcTemplate jdbcTemplate = SpringJdbcUtils.getJdbcTemplate(transactionTemplate);
int[] i = jdbcTemplate.batchUpdate(fsqls);
return i;
}
});
}
示例2: testSpringLocalTx
import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void testSpringLocalTx() throws Exception {
ConnectionFactory cf = createCF(BROKER_URL);
JmsTemplate jms = new JmsTemplate(cf);
jms.setDefaultDestinationName(QUEUE);
jms.setReceiveTimeout(1000);
PlatformTransactionManager tm = new JmsTransactionManager(cf);
TransactionTemplate localTx = new TransactionTemplate(tm);
localTx.execute(ts -> {
jms.convertAndSend("Hello");
return null;
});
Object msg = localTx.execute(ts -> jms.receiveAndConvert());
assertEquals("Hello", msg);
localTx.execute(ts -> {
jms.convertAndSend("Hello");
ts.setRollbackOnly();
return null;
});
msg = localTx.execute(ts -> jms.receiveAndConvert());
assertNull(msg);
}
示例3: testSpringXaTx
import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void testSpringXaTx() throws Exception {
DataSource ds = wrap(createHsqlDataSource());
JdbcTemplate jdbc = new JdbcTemplate(ds);
TransactionTemplate tx = new TransactionTemplate(ptm);
jdbc.execute(DROP_USER);
jdbc.execute(CREATE_TABLE_USER);
tx.execute(ts -> jdbc.update(INSERT_INTO_USER, 1, "user1"));
User user = tx.execute(ts -> jdbc.queryForObject(SELECT_FROM_USER_BY_ID, new BeanPropertyRowMapper<>(User.class), 1));
assertEquals(new User(1, "user1"), user);
tx.execute(ts -> jdbc.update(DELETE_FROM_USER_BY_ID, 1));
tx.execute(ts -> {
int nb = jdbc.update(INSERT_INTO_USER, 1, "user1");
ts.setRollbackOnly();
return nb;
});
try {
user = tx.execute(ts -> jdbc.queryForObject(SELECT_FROM_USER_BY_ID, new BeanPropertyRowMapper<>(User.class), 1));
fail("Expected a EmptyResultDataAccessException");
} catch (EmptyResultDataAccessException e) {
// expected
}
}
示例4: testSpringLocalTx
import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void testSpringLocalTx() throws Exception {
DataSource ds = wrap(createH2DataSource());
JdbcTemplate jdbc = new JdbcTemplate(ds);
TransactionTemplate tx = new TransactionTemplate(new DataSourceTransactionManager(ds));
jdbc.execute(DROP_USER);
jdbc.execute(CREATE_TABLE_USER);
tx.execute(ts -> jdbc.update(INSERT_INTO_USER, 1, "user1"));
User user = tx.execute(ts -> jdbc.queryForObject(SELECT_FROM_USER_BY_ID, new BeanPropertyRowMapper<>(User.class), 1));
assertEquals(new User(1, "user1"), user);
tx.execute(ts -> jdbc.update(DELETE_FROM_USER_BY_ID, 1));
tx.execute(ts -> {
int nb = jdbc.update(INSERT_INTO_USER, 1, "user1");
ts.setRollbackOnly();
return nb;
});
try {
user = tx.execute(ts -> jdbc.queryForObject(SELECT_FROM_USER_BY_ID, new BeanPropertyRowMapper<>(User.class), 1));
fail("Expected a EmptyResultDataAccessException");
} catch (EmptyResultDataAccessException e) {
// expected
}
}
示例5: transactionTemplateWithException
import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void transactionTemplateWithException() {
TestTransactionManager tm = new TestTransactionManager(false, true);
TransactionTemplate template = new TransactionTemplate(tm);
final RuntimeException ex = new RuntimeException("Some application exception");
try {
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
throw ex;
}
});
fail("Should have propagated RuntimeException");
}
catch (RuntimeException caught) {
// expected
assertTrue("Correct exception", caught == ex);
assertTrue("triggered begin", tm.begin);
assertTrue("no commit", !tm.commit);
assertTrue("triggered rollback", tm.rollback);
assertTrue("no rollbackOnly", !tm.rollbackOnly);
}
}
示例6: jtaTransactionManagerWithExistingTransactionAndRollbackOnlyAndNoGlobalRollback
import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void jtaTransactionManagerWithExistingTransactionAndRollbackOnlyAndNoGlobalRollback() throws Exception {
UserTransaction ut = mock(UserTransaction.class);
given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
final TransactionSynchronization synch = mock(TransactionSynchronization.class);
JtaTransactionManager ptm = newJtaTransactionManager(ut);
ptm.setGlobalRollbackOnParticipationFailure(false);
TransactionTemplate tt = new TransactionTemplate(ptm);
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);
}
示例7: testTransactionCommitWithPropagationSupports
import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void testTransactionCommitWithPropagationSupports() {
given(pmf.getPersistenceManager()).willReturn(pm);
PlatformTransactionManager tm = new JdoTransactionManager(pmf);
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
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("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
assertTrue("Is not new transaction", !status.isNewTransaction());
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
return l;
}
});
assertTrue("Correct result list", result == l);
assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
verify(pm, times(2)).close();
}
示例8: 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;
}
示例9: jtaTransactionManagerWithPropagationRequiresNewAndAdapter
import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndAdapter() throws Exception {
TransactionManager tm = mock(TransactionManager.class);
Transaction tx = mock(Transaction.class);
given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
given(tm.suspend()).willReturn(tx);
JtaTransactionManager ptm = newJtaTransactionManager(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(tm).begin();
verify(tm).commit();
verify(tm).resume(tx);
}
示例10: 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
}
}
示例11: 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());
}
示例12: 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
}
}
示例13: createFolderAction
import org.springframework.transaction.support.TransactionTemplate; //導入方法依賴的package包/類
public static Object createFolderAction(final String name, final String path, final String children,
final Long datasetId, final boolean flag, final Long bindId) {
TransactionTemplate transactionTemplate = getTransactionTemplate();
Object object = transactionTemplate.execute(new TransactionCallback<Object>() {
public Object doInTransaction(TransactionStatus status) {
int res = 0;
try {
// insert the record and get the folder id.
KeyHolder keyHolder = new GeneratedKeyHolder();
getJdbcTemplate().update(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
PreparedStatement ps = getJdbcTemplate().getDataSource().getConnection().
prepareStatement(CREATE_LOGIC_DATASET_FOLDER, new String[]{ "title" ,"path"});
ps.setString(1, name);
ps.setString(2, path);
return ps;
}
}, keyHolder);
res = keyHolder.getKey().intValue();
if (res <= 0) throw new Exception();
String childrenList = children + (children.length() == 0?children:",") + res;
int row = getJdbcTemplate().update(UPDATE_LOGIC_DATASET_CHILDREN, childrenList, datasetId);
if (row <= 0) throw new Exception();
if (!flag) {
row = getJdbcTemplate().update(UPDATE_LOGIC_DATASET_DATASETID, bindId, 0, res);
if (row <= 0) throw new Exception();
}
} catch (Exception e) {
status.setRollbackOnly();
e.printStackTrace();
}
return res;
}
});
return object;
}
示例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);
}
});
}
示例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);
}
});
}