赞
踩
Spring事务开启,常见的可以用基于注解的方式进行: @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.TransactionDefinition; @Service @Slf4j publi class serviceA(){ @Resource DataSourceTransactionManager dataSourceTransactionManager; @Resource TransactionDefinition transactionDefinition; //基于Spring手动的事务提交处理 public void methodA(){ //开启事务 TransactionStatus transaction = dataSourceTransactionManager.getTransaction(transactionDefinition); boolean isSuccess=true; try { //... }catch (Exception e){ log.error("Error",e); isSuccess=false; }finally { if(!isSuccess){ //手动回滚事务 dataSourceTransactionManager.rollback(transaction); }else { //手动提交事务 dataSourceTransactionManager.commit(transaction); } } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。