失效场景:
@Transactional(rollbackFor = Exception.class) public Map<String, Object> saveEngineer() { Map<String,Object> map = Maps.newHashMap(); try { ......... } catch (Exception e) { e.printStackTrace(); } return map; }
解决方法1:事务设置手动回滚
@Transactional(rollbackFor = Exception.class) public Map<String, Object> saveEngineer() { Map<String,Object> map = Maps.newHashMap(); try { ......... } catch (Exception e) { e.printStackTrace(); //事务设置手动回滚 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); } return map; }
解决方法2:捕获异常并将异常抛出
@Transactional(rollbackFor = Exception.class) public Map<String, Object> saveEngineer() { Map<String,Object> map = Maps.newHashMap(); try { ......... } catch (Exception e) { log.error(e.getMessage(),e); //将异常抛出 throw new ServerException(ExceptionCode.SERVER_INNER_ERROR.getExceptionCode(),e.getMessage()); } return map; }
标签:Map,Exception,map,抛出,Transactional,saveEngineer,catch,失效 From: https://www.cnblogs.com/yzhmj/p/17217938.html