zoukankan      html  css  js  c++  java
  • controller层使用@Transactional事务注解

    一般情况下,@Transactional要放在service层,并且只需要放到最外层的方法上就可以了。

    controller层使用@Transactional注解是无效的。但是可以在controller层方法的catch语句中增加:TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();语句,手动回滚,这样上层就无需去处理异常

    @RequestMapping(value = "/delrecord", method = {RequestMethod.GET})
        @Transactional(rollbackFor = Exception.class)
        public String delRecord(HttpServletRequest request) {
            try {
            //省略业务代码……
            } catch (Exception e) {
                log.error("操作异常",e);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//contoller中增加事务
                return failure("操作失败!");
            }
        }

     特别注意:如下代码发现事务不回滚,即 this.repository.delete(id); 成功把数据删除了。

        @GetMapping("delete")
        @ResponseBody
        @Transactional    
        public void delete(@RequestParam("id") int id){       
         try {            //delete country
                this.repository.delete(id);         
                   if(id == 1){              
                    throw Exception("测试事务");
                }           
                 //delete city
                this.repository.deleteByCountryId(id);
            }catch (Exception e){
                logger.error("delete false:" + e.getMessage());        
                    return new MessageBean(101,"delete false");
            }
        }

    原因:

    默认spring事务只在发生未被捕获的 RuntimeException 时才回滚。  
    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:在controller层方法的catch语句中增加:TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();语句,手动回滚,这样上层就无需去处理异常

          @GetMapping("delete") 
          @ResponseBody 
          @Transactional 
          public Object delete(@RequestParam("id") int id){ 
              if (id < 1){
                   return new MessageBean(101,"parameter wrong: id = " + id) ; 
               } 
            try { 
                  //delete country
                   this.countryRepository.delete(id);
                   //delete city
                   this.cityRepository.deleteByCountryId(id);
                   return new MessageBean(200,"delete success");
               }catch (Exception e){
                   logger.error("delete false:" + e.getMessage());
                   TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                   return new MessageBean(101,"delete false");
              }
          }
  • 相关阅读:
    leetcode——202.快乐数
    leetcode——136. 只出现一次的数字
    leetcode——74.搜索二维矩阵
    leetcode——240.搜索二维矩阵
    leetcode——414.第三大的数
    020 迭代器模式
    019 模板模式
    018 中介者模式
    017 命令模式
    016 责任链模式
  • 原文地址:https://www.cnblogs.com/zhangliang88/p/13859522.html
Copyright © 2011-2022 走看看