方法1:
在service层方法的catch语句中增加:TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();语句,手动回滚,这样上层就无需去处理异常(现在项目的做法)
方法2:
例如service层处理事务,那么service中的方法中不做异常捕获,或者在catch语句中最后增加throw new RuntimeException()语句,以便让aop捕获异常再去回滚,并且在service上层(webservice客户端,view层action)要继续捕获这个异常并处理
//普通缴费 @Transactional public String commonMoney(Receipt rpt,Moneyrecord mors){ rpt.setState(1); int a=dao.insert(rpt); if(a<=0) return"缴费失败"; mors.setPric(rpt.getPic()); mors.setExid(rpt.getPid()); mors.setState(1); boolean tf=mrs.custom(mors); if(!tf){ //事务回滚 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); return"余额不足"; } return "OK"; }
方法3:
设置回滚点进行回滚事务
//设置回滚点, Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint(); //回滚到savePoint事务点 TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);