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