美文网首页
Spring事务的7种传播行为-测试

Spring事务的7种传播行为-测试

作者: SUPER七月 | 来源:发表于2019-05-27 17:35 被阅读0次

    Role代码块

        @Override
        @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
        public int testTranstationREQUIRED()  throws RuntimeException {
            SysRole role = new SysRole();
            role.setRoleName("test");
            role.setRoleCode("test");
            this.roleMapper.insert(role);
            return 1;
        }
        @Override
        @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
        public int testTranstationSUPPORTS() throws RuntimeException {
            SysRole role = new SysRole();
            role.setRoleName("test");
            role.setRoleCode("test");
            this.roleMapper.insert(role);
            return 1;
        }
        @Override
        @Transactional(propagation = Propagation.MANDATORY,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
        public int testTranstationMANDATORY()  throws RuntimeException{
            SysRole role = new SysRole();
            role.setRoleName("test");
            role.setRoleCode("test");
            this.roleMapper.insert(role);
            return 1;
        }
        @Override
        @Transactional(propagation = Propagation.REQUIRES_NEW,isolation = Isolation.DEFAULT,rollbackFor = RuntimeException.class)
        public int testTranstationREQUIRES_NEW() throws RuntimeException {
            SysRole role = new SysRole();
            role.setRoleName("test");
            role.setRoleCode("test");
            this.roleMapper.insert(role);
            return 1;
        }
        @Override
        @Transactional(propagation = Propagation.NOT_SUPPORTED,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
        public int testTranstationNOT_SUPPORTED() throws RuntimeException {
            SysRole role = new SysRole();
            role.setRoleName("test");
            role.setRoleCode("test");
            this.roleMapper.insert(role);
            return 1;
        }
        @Override
        @Transactional(propagation = Propagation.NEVER,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
        public int testTranstationNEVER() throws RuntimeException {
            SysRole role = new SysRole();
            role.setRoleName("test");
            role.setRoleCode("test");
            this.roleMapper.insert(role);
            return 1;
        }
        @Override
        @Transactional(propagation = Propagation.NESTED,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
        public int testTranstationNESTED() throws RuntimeException {
            SysRole role = new SysRole();
            role.setRoleName("test");
            role.setRoleCode("test");
            this.roleMapper.insert(role);
            return 1;
        }
    

    User代码块和对应测试结果

        @Override
        @Transactional(propagation = Propagation.NESTED,isolation = Isolation.DEFAULT,readOnly = false,rollbackFor = Exception.class)
        public int testTranstation(int type)  throws Exception{
            SysUser user = new SysUser();
            user.setUserName("test");
            user.setPassWord("123");
            user.setRegisterDate(new Date());
            this.userMapper.insert(user);
            if(type==1)roleService.testTranstationREQUIRED();// user回滚 role回滚
            if(type==2)roleService.testTranstationSUPPORTS();// user回滚 role回滚
            if(type==3)roleService.testTranstationMANDATORY();// user回滚 role回滚
            if(type==4)roleService.testTranstationREQUIRES_NEW();// user回滚 role不回滚
            if(type==5)roleService.testTranstationNOT_SUPPORTED();// user回滚 role不回滚
            if(type==6)roleService.testTranstationNEVER();// user回滚 role异常
            if(type==7)roleService.testTranstationNESTED();// user回滚 role回滚
            int a = 1/0;
            return 1;
        }
        @Transactional(propagation = Propagation.NEVER,isolation = Isolation.DEFAULT,readOnly = false,rollbackFor = Exception.class)
        public int testTranstation6(int type)  throws Exception{
            SysUser user = new SysUser();
            user.setUserName("test");
            user.setPassWord("123");
            user.setRegisterDate(new Date());
            this.userMapper.insert(user);
            if(type==1)roleService.testTranstationREQUIRED();// user不回滚 role不回滚
            if(type==2)roleService.testTranstationSUPPORTS();// user不回滚 role不回滚
            if(type==3)roleService.testTranstationMANDATORY();// user不回滚 role异常
            if(type==4)roleService.testTranstationREQUIRES_NEW();// user不回滚 role不回滚
            if(type==5)roleService.testTranstationNOT_SUPPORTED();// user不回滚 role不回滚
            if(type==6)roleService.testTranstationNEVER();// user不回滚 role不回滚
            if(type==7)roleService.testTranstationNESTED();// user不回滚 role不回滚
            int a = 1/0;
            return 1;
        }
        @Transactional(propagation = Propagation.NOT_SUPPORTED,isolation = Isolation.DEFAULT,readOnly = false,rollbackFor = Exception.class)
        public int testTranstation5(int type)  throws Exception{
            SysUser user = new SysUser();
            user.setUserName("test");
            user.setPassWord("123");
            user.setRegisterDate(new Date());
            this.userMapper.insert(user);
            if(type==1)roleService.testTranstationREQUIRED();// user不回滚 role不回滚
            if(type==2)roleService.testTranstationSUPPORTS();// user不回滚 role不回滚
            if(type==3)roleService.testTranstationMANDATORY();// user不回滚 role异常
            if(type==4)roleService.testTranstationREQUIRES_NEW();// user不回滚 role不回滚
            if(type==5)roleService.testTranstationNOT_SUPPORTED();// user不回滚 role不回滚
            if(type==6)roleService.testTranstationNEVER();// user不回滚 role不回滚
            if(type==7)roleService.testTranstationNESTED();// user不回滚 role不回滚
            int a = 1/0;
            return 1;
        }
        @Transactional(propagation = Propagation.REQUIRES_NEW,isolation = Isolation.DEFAULT,readOnly = false,rollbackFor = Exception.class)
        public int testTranstation4(int type)  throws Exception{
            SysUser user = new SysUser();
            user.setUserName("test");
            user.setPassWord("123");
            user.setRegisterDate(new Date());
            this.userMapper.insert(user);
            if(type==1)roleService.testTranstationREQUIRED();// user回滚 role回滚
            if(type==2)roleService.testTranstationSUPPORTS();// user回滚 role回滚
            if(type==3)roleService.testTranstationMANDATORY();// user回滚 role回滚
            if(type==4)roleService.testTranstationREQUIRES_NEW();// user回滚 role不回滚
            if(type==5)roleService.testTranstationNOT_SUPPORTED();// user回滚 role不回滚
            if(type==6)roleService.testTranstationNEVER();// user回滚 role异常
            if(type==7)roleService.testTranstationNESTED();// user回滚 role回滚
            int a = 1/0;
            return 1;
        }
        @Transactional(propagation = Propagation.MANDATORY,isolation = Isolation.DEFAULT,readOnly = false,rollbackFor = Exception.class)
        public int testTranstation3(int type)  throws Exception{
            //不存在事务,直接抛异常,没有进入方法
            SysUser user = new SysUser();
            user.setUserName("test");
            user.setPassWord("123");
            user.setRegisterDate(new Date());
            this.userMapper.insert(user);
            if(type==1)roleService.testTranstationREQUIRED();
            if(type==2)roleService.testTranstationSUPPORTS();
            if(type==3)roleService.testTranstationMANDATORY();
            if(type==4)roleService.testTranstationREQUIRES_NEW();
            if(type==5)roleService.testTranstationNOT_SUPPORTED();
            if(type==6)roleService.testTranstationNEVER();
            if(type==7)roleService.testTranstationNESTED();
            int a = 1/0;
            return 1;
        }
        @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,readOnly = false,rollbackFor = Exception.class)
        public int testTranstation2(int type)  throws Exception{
            SysUser user = new SysUser();
            user.setUserName("test");
            user.setPassWord("123");
            user.setRegisterDate(new Date());
            this.userMapper.insert(user);
            if(type==1)roleService.testTranstationREQUIRED();// user不回滚 role不回滚
            if(type==2)roleService.testTranstationSUPPORTS();// user不回滚 role不回滚
            if(type==3)roleService.testTranstationMANDATORY();// user不回滚 role回滚
            if(type==4)roleService.testTranstationREQUIRES_NEW();// user不回滚 role不回滚
            if(type==5)roleService.testTranstationNOT_SUPPORTED();// user不回滚 role不回滚
            if(type==6)roleService.testTranstationNEVER();// user不回滚 role不回滚
            if(type==7)roleService.testTranstationNESTED();// user不回滚 role不回滚
            int a = 1/0;
            return 1;
        }
        @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,readOnly = false,rollbackFor = Exception.class)
        public int testTranstation1(int type)  throws Exception{
            SysUser user = new SysUser();
            user.setUserName("test");
            user.setPassWord("123");
            user.setRegisterDate(new Date());
            this.userMapper.insert(user);
            if(type==1)roleService.testTranstationREQUIRED();// user回滚 role回滚
            if(type==2)roleService.testTranstationSUPPORTS();// user回滚 role回滚
            if(type==3)roleService.testTranstationMANDATORY();// user回滚 role回滚
            if(type==4)roleService.testTranstationREQUIRES_NEW();// user回滚 role不回滚
            if(type==5)roleService.testTranstationNOT_SUPPORTED();// user回滚 role不回滚
            if(type==6)roleService.testTranstationNEVER();// user回滚 role回滚
            if(type==7)roleService.testTranstationNESTED();// user回滚 role回滚
            int a = 1/0;
            return 1;
        }
    

    相关文章

      网友评论

          本文标题:Spring事务的7种传播行为-测试

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