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