本文整理汇总了Java中org.springframework.transaction.support.TransactionCallback类的典型用法代码示例。如果您正苦于以下问题:Java TransactionCallback类的具体用法?Java TransactionCallback怎么用?Java TransactionCallback使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TransactionCallback类属于org.springframework.transaction.support包,在下文中一共展示了TransactionCallback类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: updateBook
import org.springframework.transaction.support.TransactionCallback; //导入依赖的package包/类
@Override
public boolean updateBook(long ISBN, int price) {
// TODO Auto-generated method stub
if (searchBook(ISBN).getISBN() == ISBN) {
transactionTemplate.setReadOnly(false);
return transactionTemplate.execute(new TransactionCallback<Boolean>() {
@Override
public Boolean doInTransaction(TransactionStatus status) {
// TODO Auto-generated method stub
try {
int rows = bookDAO.updateBook(ISBN, price);
if (rows > 0)
return true;
} catch (Exception exception) {
status.setRollbackOnly();
}
return false;
}
});
}
return false;
}
示例2: invoke
import org.springframework.transaction.support.TransactionCallback; //导入依赖的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.TransactionCallback; //导入依赖的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.TransactionCallback; //导入依赖的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.TransactionCallback; //导入依赖的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.TransactionCallback; //导入依赖的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: testRwRouteWithTransaction2
import org.springframework.transaction.support.TransactionCallback; //导入依赖的package包/类
@Test
public void testRwRouteWithTransaction2() {
mapper.findByStatus((short) 1);
transactionTemplate.execute(new TransactionCallback<Void>() {
@Override
public Void doInTransaction(TransactionStatus status) {
mapper.findByStatus((short) 2);
UserEntity entity = new UserEntity();
entity.setCreatedAt(new Date());
entity.setEmail(RandomStringUtils.random(6, true, true) + "@163.com");
entity.setMobile("13800" + RandomUtils.nextLong(100000, 999999));
entity.setType((short) 1);
entity.setStatus((short) 1);
mapper.insert(entity);
mapper.findByStatus((short) 2);
return null;
}
});
System.out.println();
}
示例8: run
import org.springframework.transaction.support.TransactionCallback; //导入依赖的package包/类
public synchronized void run(boolean useIndex) {
if (useIndex) {
jdbcTemplate.query(sql, setter, rowHandler);
} else {
long latest = latestFetcher.retrieveLatestSer();
transactionTemplate.execute(new TransactionCallback<Object>() {
@Override
public Object doInTransaction(TransactionStatus status) {
jdbcTemplate.execute("SET LOCAL enable_bitmapscan=0;");
jdbcTemplate.query(sql, setter, rowHandler);
return null;
}
});
// shift to max(retrievedLatestSer, and ser as updated in
// rowHandler)
serialToContinueFrom.set(Math.max(serialToContinueFrom.get(), latest));
}
}
示例9: searchBook
import org.springframework.transaction.support.TransactionCallback; //导入依赖的package包/类
@Override
public Book searchBook(long ISBN) {
// TODO Auto-generated method stub
transactionTemplate.setReadOnly(true);
return transactionTemplate.execute(new TransactionCallback<Book>() {
@Override
public Book doInTransaction(TransactionStatus status) {
// TODO Auto-generated method stub
// Book book = bookDAO.serachBook(ISBN);
Book book=new Book();
book.setISBN(ISBN);
bookDAO.addBook(book);
return book;
}
});
}
示例10: deleteBook
import org.springframework.transaction.support.TransactionCallback; //导入依赖的package包/类
@Override
public boolean deleteBook(long ISBN) {
// TODO Auto-generated method stub
if (searchBook(ISBN).getISBN() == ISBN) {
transactionTemplate.setReadOnly(false);
return transactionTemplate.execute(new TransactionCallback<Boolean>() {
@Override
public Boolean doInTransaction(TransactionStatus status) {
// TODO Auto-generated method stub
try {
boolean result = bookDAO.deleteBook(ISBN);
return result;
} catch (Exception exception) {
status.setRollbackOnly();
}
return false;
}
});
}
return false;
}
示例11: updateSql
import org.springframework.transaction.support.TransactionCallback; //导入依赖的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;
}
});
}
示例12: writeIntoFile
import org.springframework.transaction.support.TransactionCallback; //导入依赖的package包/类
public boolean writeIntoFile(final File file) {
return transactionHelper.runInCurrentThread(new TransactionCallback<Boolean>() {
@Override
public Boolean doInTransaction(final TransactionStatus status) {
status.setRollbackOnly();
try {
final Image image = imageService.load(id);
final InputStream in = new ByteArrayInputStream(isThumbnail ? image.getThumbnail() : image.getImage());
file.getParentFile().mkdirs();
IOUtils.copy(in, new FileOutputStream(file));
file.setLastModified(image.getLastModified().getTimeInMillis());
return true;
} catch (final Exception e) {
// Ignore
}
return false;
}
});
}
示例13: testTransactionCommitWithSharedEntityManager
import org.springframework.transaction.support.TransactionCallback; //导入依赖的package包/类
@Test
public void testTransactionCommitWithSharedEntityManager() {
given(manager.getTransaction()).willReturn(tx);
tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
bean.sharedEntityManager.flush();
return null;
}
});
verify(tx).commit();
verify(manager).flush();
verify(manager).close();
}
示例14: testTransactionCommitWithSharedEntityManagerAndPropagationSupports
import org.springframework.transaction.support.TransactionCallback; //导入依赖的package包/类
@Test
public void testTransactionCommitWithSharedEntityManagerAndPropagationSupports() {
given(manager.isOpen()).willReturn(true);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
bean.sharedEntityManager.clear();
return null;
}
});
verify(manager).clear();
verify(manager).close();
}
示例15: testTransactionCommitWithExtendedEntityManager
import org.springframework.transaction.support.TransactionCallback; //导入依赖的package包/类
@Test
public void testTransactionCommitWithExtendedEntityManager() {
given(manager.getTransaction()).willReturn(tx);
tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
bean.extendedEntityManager.flush();
return null;
}
});
verify(tx, times(2)).commit();
verify(manager).flush();
verify(manager).close();
}