美文网首页
springboot事务失效解决-TransactionAspe

springboot事务失效解决-TransactionAspe

作者: Raral | 来源:发表于2021-12-06 14:23 被阅读0次

    事务失效解决

    一 失效原因和写法(同一个类,controller调用有事务的方法)

    @Override
        @Transactional(rollbackFor = CommonExcpetion.class)
        public RespDTO updateTest(ReqDTO reqDTO) throws CommonExcpetion {
            RespDTO instance = RespDTO.getInstance();
    
            try {
                this.updateRecord("0123", "7890");
                this.updateAtvIssuer("0123", "7890");
            } catch (CommonExcpetion ex) {
                instance.setCode(ex.getCode());
                instance.setMsg(ex.getMsg());
    //            instance
                return instance;
            }
    
            return RespDTO.getInstance();
        }
    
    
        public void updateRecord(String ino, String atvId) throws CommonExcpetion{
    
            AtvRecordPO atvRecordPO = new AtvRecordPO();
            atvRecordPO.setAtvId(atvId);
            atvRecordPO.setIno(ino);
            atvRecordPO.setIssuerName("test");
            try {
                boolean save = this.save(atvRecordPO);
                if(!save) {
                    log.error("test-插入记录失败");
                    throw new CommonExcpetion("230", "插入记录失败");
                }
            } catch (Exception e) {
                log.error("test-插入记录异常");
       
                throw new CommonExcpetion("230", "插入记录异常");
            }
    
        }
        public void updateAtvIssuer(String ino, String atvId) throws CommonExcpetion {
            AtvIssuerPO atvIssuerPO = new AtvIssuerPO();
            atvIssuerPO.setAtvId(atvId);
            atvIssuerPO.setIno(ino);
            atvIssuerPO.setIssuerName("test");
            try {
                boolean save = iAtvIssuerService.save(atvIssuerPO);
                int i = 5/ 0;
                if(!save) {
                    log.error("test-插入券商失败");
                 
                    throw new CommonExcpetion("230", "插入券商失败");
                }
            } catch (Exception e) {
                log.error("test-插入券商异常");
             
                throw new CommonExcpetion("230", "插入券商异常");
            }
        }
    

    一 事务生效解决(调用事务的方法)

    1. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
     @Override
        @Transactional(rollbackFor = CommonExcpetion.class)
        public RespDTO updateTest(ReqDTO reqDTO) throws CommonExcpetion {
    //        AtvRecordServiceImpl atvRecordService = (AtvRecordServiceImpl) AopContext.currentProxy();
            RespDTO instance = RespDTO.getInstance();
    
            try {
                this.updateRecord("0123", "7890");
                this.updateAtvIssuer("0123", "7890");
            } catch (CommonExcpetion ex) {
                instance.setCode(ex.getCode());
                instance.setMsg(ex.getMsg());
    //            instance
                return instance;
            }
    
            return RespDTO.getInstance();
        }
    
    
        public void updateRecord(String ino, String atvId) throws CommonExcpetion{
    
            AtvRecordPO atvRecordPO = new AtvRecordPO();
            atvRecordPO.setAtvId(atvId);
            atvRecordPO.setIno(ino);
            atvRecordPO.setIssuerName("test");
            try {
                boolean save = this.save(atvRecordPO);
                if(!save) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    log.error("test-插入记录失败");
                    throw new CommonExcpetion("230", "插入记录失败");
                }
            } catch (Exception e) {
                log.error("test-插入记录异常");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new CommonExcpetion("230", "插入记录异常");
            }
    
        }
        public void updateAtvIssuer(String ino, String atvId) throws CommonExcpetion {
            AtvIssuerPO atvIssuerPO = new AtvIssuerPO();
            atvIssuerPO.setAtvId(atvId);
            atvIssuerPO.setIno(ino);
            atvIssuerPO.setIssuerName("test");
            try {
                boolean save = iAtvIssuerService.save(atvIssuerPO);
                int i = 5/ 0; //模拟异常
                if(!save) {
                    log.error("test-插入券商失败");
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    throw new CommonExcpetion("230", "插入券商失败");
                }
            } catch (Exception e) {
                log.error("test-插入券商异常");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new CommonExcpetion("230", "插入券商异常");
            }
        }
    
    1. 加一层try catch(不推荐使用)
    @Override
        @Transactional(rollbackFor = CommonExcpetion.class)
        public RespDTO updateTest(ReqDTO reqDTO) throws CommonExcpetion {
    
            RespDTO instance = RespDTO.getInstance();
            try {
                this.updateRecord("0123", "7890");
            } catch (CommonExcpetion commonExcpetion) {
               throw commonExcpetion;
            }
    
            try {
                this.updateAtvIssuer("0123", "7890");
            } catch (CommonExcpetion commonExcpetion) {
                throw commonExcpetion;
            }
    
    
            return RespDTO.getInstance();
        }
    
    
        public void updateRecord(String ino, String atvId) throws CommonExcpetion{
    
            AtvRecordPO atvRecordPO = new AtvRecordPO();
            atvRecordPO.setAtvId(atvId);
            atvRecordPO.setIno(ino);
            atvRecordPO.setIssuerName("test");
            try {
                boolean save = this.save(atvRecordPO);
                if(!save) {
    //                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    log.error("test-插入记录失败");
                    throw new CommonExcpetion("230", "插入记录失败");
                }
            } catch (Exception e) {
                log.error("test-插入记录异常");
    //            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new CommonExcpetion("230", "插入记录异常");
            }
    
        }
        public void updateAtvIssuer(String ino, String atvId) throws CommonExcpetion {
            AtvIssuerPO atvIssuerPO = new AtvIssuerPO();
            atvIssuerPO.setAtvId(atvId);
            atvIssuerPO.setIno(ino);
            atvIssuerPO.setIssuerName("test");
            try {
                boolean save = iAtvIssuerService.save(atvIssuerPO);
                int i = 5/ 0; //模拟异常
                if(!save) {
                    log.error("test-插入券商失败");
    //                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    throw new CommonExcpetion("230", "插入券商失败");
                }
            } catch (Exception e) {
                log.error("test-插入券商异常");
    //            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new CommonExcpetion("230", "插入券商异常");
            }
        }
    

    二 失效原因和写法(同一个类,controller调用没有事务的方法)

     @Override
        @Transactional(rollbackFor = CommonExcpetion.class)
        public RespDTO updateTest(ReqDTO reqDTO) throws CommonExcpetion {
    
            RespDTO instance = RespDTO.getInstance();
            try {
                this.updateRecord("0123", "7890");
            } catch (CommonExcpetion commonExcpetion) {
               throw commonExcpetion;
            }
    
            try {
                this.updateAtvIssuer("0123", "7890");
            } catch (CommonExcpetion commonExcpetion) {
                throw commonExcpetion;
            }
    
    
            return RespDTO.getInstance();
        }
    
    
        public void updateRecord(String ino, String atvId) throws CommonExcpetion{
    
            AtvRecordPO atvRecordPO = new AtvRecordPO();
            atvRecordPO.setAtvId(atvId);
            atvRecordPO.setIno(ino);
            atvRecordPO.setIssuerName("test");
            try {
                boolean save = this.save(atvRecordPO);
                if(!save) {
    //                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    log.error("test-插入记录失败");
                    throw new CommonExcpetion("230", "插入记录失败");
                }
            } catch (Exception e) {
                log.error("test-插入记录异常");
    //            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new CommonExcpetion("230", "插入记录异常");
            }
    
        }
        public void updateAtvIssuer(String ino, String atvId) throws CommonExcpetion {
            AtvIssuerPO atvIssuerPO = new AtvIssuerPO();
            atvIssuerPO.setAtvId(atvId);
            atvIssuerPO.setIno(ino);
            atvIssuerPO.setIssuerName("test");
            try {
                boolean save = iAtvIssuerService.save(atvIssuerPO);
                int i = 5/ 0; //模拟异常
                if(!save) {
                    log.error("test-插入券商失败");
    //                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    throw new CommonExcpetion("230", "插入券商失败");
                }
            } catch (Exception e) {
                log.error("test-插入券商异常");
    //            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new CommonExcpetion("230", "插入券商异常");
            }
        }
        //没有事务的方法
        public RespDTO updateTest2(ReqDTO reqDTO) throws CommonExcpetion {
            return this.updateTest(reqDTO);
        }
    

    二 事务生效解决(调用没有事务的方法,里面调用事务的方法)

    1. AtvRecordServiceImpl atvRecordService = (AtvRecordServiceImpl)AopContext.currentProxy();

    一个没有事务的方法,调用有事务的方法,事务会失效的,通过AopContext.currentProxy();解决

    
        @Override
        @Transactional(rollbackFor = CommonExcpetion.class)
        public RespDTO updateTest(ReqDTO reqDTO) throws CommonExcpetion {
    //        AtvRecordServiceImpl atvRecordService = (AtvRecordServiceImpl) AopContext.currentProxy();
            RespDTO instance = RespDTO.getInstance();
            try {
                this.updateRecord("0123", "7890");
            } catch (CommonExcpetion commonExcpetion) {
               throw commonExcpetion;
            }
    
            try {
                this.updateAtvIssuer("0123", "7890");
            } catch (CommonExcpetion commonExcpetion) {
                throw commonExcpetion;
            }
    
    
            return RespDTO.getInstance();
        }
    
    
        public void updateRecord(String ino, String atvId) throws CommonExcpetion{
    
            AtvRecordPO atvRecordPO = new AtvRecordPO();
            atvRecordPO.setAtvId(atvId);
            atvRecordPO.setIno(ino);
            atvRecordPO.setIssuerName("test");
            try {
                boolean save = this.save(atvRecordPO);
                if(!save) {
    //                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    log.error("test-插入记录失败");
                    throw new CommonExcpetion("230", "插入记录失败");
                }
            } catch (Exception e) {
                log.error("test-插入记录异常");
    //            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new CommonExcpetion("230", "插入记录异常");
            }
    
        }
        public void updateAtvIssuer(String ino, String atvId) throws CommonExcpetion {
            AtvIssuerPO atvIssuerPO = new AtvIssuerPO();
            atvIssuerPO.setAtvId(atvId);
            atvIssuerPO.setIno(ino);
            atvIssuerPO.setIssuerName("test");
            try {
                boolean save = iAtvIssuerService.save(atvIssuerPO);
                int i = 5/ 0; //模拟异常
                if(!save) {
                    log.error("test-插入券商失败");
    //                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    throw new CommonExcpetion("230", "插入券商失败");
                }
            } catch (Exception e) {
                log.error("test-插入券商异常");
    //            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new CommonExcpetion("230", "插入券商异常");
            }
        }
        //没有事务的方法
        public RespDTO updateTest2(ReqDTO reqDTO) throws CommonExcpetion {
            AtvRecordServiceImpl atvRecordService =   (AtvRecordServiceImpl)AopContext.currentProxy();
            return atvRecordService.updateTest(reqDTO);
        }
    

    相关文章

      网友评论

          本文标题:springboot事务失效解决-TransactionAspe

          本文链接:https://www.haomeiwen.com/subject/irngxrtx.html