springboot事务失效解决-TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()、AopContext...

事务失效解决

一 失效原因和写法(同一个类,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);
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容