本文整理汇总了Java中org.springframework.transaction.support.TransactionTemplate类的典型用法代码示例。如果您正苦于以下问题:Java TransactionTemplate类的具体用法?Java TransactionTemplate怎么用?Java TransactionTemplate使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TransactionTemplate类属于org.springframework.transaction.support包,在下文中一共展示了TransactionTemplate类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: AbstractDbDialect
import org.springframework.transaction.support.TransactionTemplate; //导入依赖的package包/类
public AbstractDbDialect(final JdbcTemplate jdbcTemplate, LobHandler lobHandler){
this.jdbcTemplate = jdbcTemplate;
this.lobHandler = lobHandler;
// 初始化transction
this.transactionTemplate = new TransactionTemplate();
transactionTemplate.setTransactionManager(new DataSourceTransactionManager(jdbcTemplate.getDataSource()));
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
// 初始化一些数据
jdbcTemplate.execute(new ConnectionCallback() {
public Object doInConnection(Connection c) throws SQLException, DataAccessException {
DatabaseMetaData meta = c.getMetaData();
databaseName = meta.getDatabaseProductName();
databaseMajorVersion = meta.getDatabaseMajorVersion();
databaseMinorVersion = meta.getDatabaseMinorVersion();
return null;
}
});
initTables(jdbcTemplate);
}
示例2: invoke
import org.springframework.transaction.support.TransactionTemplate; //导入依赖的package包/类
@Override
public EasyTransResult invoke(EasyTransFilterChain filterChain, Map<String, Object> header,
EasyTransRequest<?, ?> request) {
Integer pTrxStatus = MetaDataFilter.getMetaData(EasytransConstant.CallHeadKeys.PARENT_TRANSACTION_STATUS);
if(!pTrxStatus.equals(com.yiqiniu.easytrans.datasource.TransStatusLogger.TransactionStatus.UNKNOWN)){
// start transaction to update
PlatformTransactionManager transactionManager = getTransactionManager(filterChain, request);
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager,
new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED));
TransactionId pTrxId = MetaDataFilter.getMetaData(EasytransConstant.CallHeadKeys.PARENT_TRX_ID_KEY);
transactionTemplate.execute(new TransactionCallback<Object>() {
@Override
public Object doInTransaction(TransactionStatus status) {
TransactionId trxId = pTrxId;
transStatusLogger.updateExecuteFlagForSlaveTrx(trxId, request, pTrxStatus);
return null;
}
});
boolean commited = pTrxStatus.equals(com.yiqiniu.easytrans.datasource.TransStatusLogger.TransactionStatus.COMMITTED);
//may be concurrent,but it's ok
easyTransSynchronizer.cascadeExecuteCachedTransaction(pTrxId, commited);
}
return filterChain.invokeFilterChain(header, request);
}
示例3: getTicketInTransaction
import org.springframework.transaction.support.TransactionTemplate; //导入依赖的package包/类
Ticket getTicketInTransaction(final String ticketId) {
return new TransactionTemplate(txManager).execute(new TransactionCallback<Ticket>() {
@Override
public Ticket doInTransaction(final TransactionStatus status) {
return jpaTicketRegistry.getTicket(ticketId);
}
});
}
示例4: invoke
import org.springframework.transaction.support.TransactionTemplate; //导入依赖的package包/类
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
return new TransactionTemplate(txManager).execute(new TransactionCallback<Object>() {
@Override
public Object doInTransaction(final TransactionStatus status) {
try {
final Object result = method.invoke(jpaLock, args);
jpaLock.entityManager.flush();
logger.debug("Performed {} on {}", method.getName(), jpaLock);
return result;
// Force result of transaction to database
} catch (final Exception e) {
throw new RuntimeException("Transactional method invocation failed.", e);
}
}
});
}
示例5: invoke
import org.springframework.transaction.support.TransactionTemplate; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
return new TransactionTemplate(txManager).execute(new TransactionCallback<Object>() {
public Object doInTransaction(final TransactionStatus status) {
try {
final Object result = method.invoke(jpaLock, args);
jpaLock.entityManager.flush();
logger.debug("Performed {} on {}", method.getName(), jpaLock);
return result;
// Force result of transaction to database
} catch (final Exception e) {
throw new RuntimeException("Transactional method invocation failed.", e);
}
}
});
}
示例6: invoke
import org.springframework.transaction.support.TransactionTemplate; //导入依赖的package包/类
/** {@inheritDoc} */
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
return new TransactionTemplate(txManager).execute(new TransactionCallback<Object>() {
public Object doInTransaction(final TransactionStatus status) {
try {
final Object result = method.invoke(jpaLock, args);
jpaLock.entityManager.flush();
logger.debug("Performed {} on {}", method.getName(), jpaLock);
return result;
// Force result of transaction to database
} catch (final Exception e) {
throw new RuntimeException("Transactional method invocation failed.", e);
}
}
});
}
示例7: 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);
}
示例8: 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
}
}
示例9: testSpringLocalTx
import org.springframework.transaction.support.TransactionTemplate; //导入依赖的package包/类
@Test
public void testSpringLocalTx() throws Exception {
DataSource ds = wrap(createHsqlDataSource());
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
}
}
示例10: testSpringXaTx
import org.springframework.transaction.support.TransactionTemplate; //导入依赖的package包/类
@Test
public void testSpringXaTx() throws Exception {
DataSource ds = wrap(createH2DataSource());
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
}
}
示例11: 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
}
}
示例12: 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;
}
});
}
示例13: shouldRollbackOnCursorManagerException
import org.springframework.transaction.support.TransactionTemplate; //导入依赖的package包/类
@Test
public void shouldRollbackOnCursorManagerException() throws IOException {
new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() {
@Override
public Void doInTransaction(TransactionStatus transactionStatus) {
assertFalse("Transaction should be active", transactionStatus.isRollbackOnly());
try {
batchHandler.processBatch(new IORunnable() {
@Override
public void run() throws IOException {
throw new IOException("commit failed");
}
});
} catch (IOException e) {
LOG.info("Ignoring [{}] with message [{}]", e.getClass().getName(), e.getMessage());
}
assertTrue("Transaction should be marked as rollback only", transactionStatus.isRollbackOnly());
return null;
}
});
}
示例14: setUp
import org.springframework.transaction.support.TransactionTemplate; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
factory = mock(EntityManagerFactory.class);
manager = mock(EntityManager.class);
tx = mock(EntityTransaction.class);
JpaTransactionManager tm = new JpaTransactionManager(factory);
tt = new TransactionTemplate(tm);
given(factory.createEntityManager()).willReturn(manager);
given(manager.getTransaction()).willReturn(tx);
given(manager.isOpen()).willReturn(true);
bean = new EntityManagerHoldingBean();
@SuppressWarnings("serial")
PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor() {
@Override
protected EntityManagerFactory findEntityManagerFactory(String unitName, String requestingBeanName) {
return factory;
}
};
pabpp.postProcessPropertyValues(null, null, bean, "bean");
assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
示例15: 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();
}