MyException 自定义异常类
public class MyException extends Exception{ private static final long serialVersionUID = 1L; private String message; @Override public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public MyException(String message) { this.message = message; } public MyException(String message, String message1) { super(message); this.message = message1; } public MyException(String message, Throwable cause, String message1) { super(message, cause); this.message = message1; } public MyException(Throwable cause, String message) { super(cause); this.message = message; } public MyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, String message1) { super(message, cause, enableSuppression, writableStackTrace); this.message = message1; } }
ProService.java
@Transactional(readOnly = false, propagation = Propagation.REQUIRED ,isolation= Isolation.READ_COMMITTED,timeout=5) public BaseResult<String> updateCoreChannel(CoreChannel record) { BaseResult<String> baseResult = new BaseResult<String>(); baseResult.setReturnCode(ReturnMsg.SUCCESS.getCode()); baseResult.setReturnMsg(ReturnMsg.SUCCESS.getMsg()); //暂时关闭 3 级渠道 if(record.getParentId()!=null && record.getParentId()>0){ List<CoreChannel> lists = getCoreChannelById(record.getParentId()); if(lists!=null && lists.size()>0){ if(lists.get(0).getParentId()!=null){ baseResult.setReturnCode(ReturnMsg.UNKONOW_ERROR.getCode()); baseResult.setReturnMsg("暂不支持添加3级或以上渠道"); return baseResult; } } } Integer result = null; try { result = coreChannelMapper.updateByPrimaryKeySelective(record); //2019-11-08 老系统中同步 修改 b2b_agent 、 b2b_customer 、b2b_customer_webuser if (result > 0) { baseResult = updateAgentAndCustomerAndWebuser(record); if(!ReturnMsg.SUCCESS.getCode().equals(baseResult.getReturnCode())){ throw new MyException(baseResult.getReturnMsg()); } }else{ throw new MyException("修改渠道失败!"); } } catch (MyException e) { // e.printStackTrace(); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); baseResult.setReturnCode(ReturnMsg.UNKONOW_ERROR.getCode()); baseResult.setReturnMsg(e.getMessage()); }catch (Exception e){ TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); baseResult.setReturnCode(ReturnMsg.UNKONOW_ERROR.getCode()); baseResult.setReturnMsg(e.getMessage()); } return baseResult; }
controller
public BaseResult<String> updateCoreChannel(@RequestBody CoreChannel record) { record.setModifyDate(new Date()); return userCenterService.updateCoreChannel(record); }
service 中已对抛出的异常进行了手动处理了(加了try catch)
手动回滚 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
注意:
*****默认spring事务只在发生未被捕获的 runtimeexcetpion时才回滚。 ****** spring aop 异常捕获原理:被拦截的方法需显式抛出异常,并不能经任何处理,这样aop代理才能捕获到方法的异常,才能进行回滚,默认情况下aop只捕获runtimeexception的异常,但可以通过 。 配置来捕获特定的异常并回滚 换句话说在service的方法中不使用try catch 或者在catch中最后加上throw new runtimeexcetpion(),这样程序异常时才能被aop捕获进而回滚 解决方案: 方案1.例如service层处理事务,那么service中的方法中不做异常捕获,或者在catch语句中最后增加throw new RuntimeException()语句,以便让aop捕获异常再去回滚,并且在service上层(webservice客户端,view层action)要继续捕获这个异常并处理 方案2.在service层方法的catch语句中增加:TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();语句,手动回滚,这样上层就无需去处理异常(现在项目的做法)。