• spring 隔离级别 测试代码


    @Controller
    @RequestMapping("/test")
    @Api(value = "测试", description = "测试")
    public class TestController {
    
        @Autowired
        private TestService testService;
    
        @RequestMapping(value = "listForDirtyRead", method = RequestMethod.GET)
        @ResponseBody
        @ApiImplicitParams({})
        @ApiOperation(value="listForDirtyRead")
        public Object listForDirtyRead() {
    
            return testService.listForDirtyRead();
        }
    
    
        @RequestMapping(value = "insertForDirtyReadAndIllusion", method = RequestMethod.POST)
        @ResponseBody
        @ApiImplicitParams({})
        @ApiOperation(value="insertForDirtyReadAndIllusion")
        public void insertForDirtyReadAndIllusion() {
    
             testService.insertForDirtyReadAndIllusion();
        }
    
        @RequestMapping(value = "listForIllusionRead", method = RequestMethod.GET)
        @ResponseBody
        @ApiImplicitParams({})
        @ApiOperation(value="listForIllusionRead")
        public Object listForIllusionRead() {
    
            return testService.listForIllusionRead();
        }
    
        @RequestMapping(value = "updateForNoRepeat", method = RequestMethod.POST)
        @ResponseBody
        @ApiImplicitParams({})
        @ApiOperation(value="updateForNoRepeat")
        public void updateForNoRepeat() {
    
            testService.updateForNoRepeat();
        }
    
        @RequestMapping(value = "deleteForNoRepeat", method = RequestMethod.POST)
        @ResponseBody
        @ApiImplicitParams({})
        @ApiOperation(value="deleteForNoRepeat")
        public void deleteForNoRepeat() {
    
            testService.deleteForNoRepeat();
        }
    }
    @Service
    public class TestService {
    
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        @Transactional(isolation = Isolation.READ_COMMITTED)
        public List<Map<String,Object>> listForDirtyRead() {
            List<Map<String,Object>> map = jdbcTemplate.queryForList("select * from tao");
            return map;
        }
    
        @Transactional
        public void insertForDirtyReadAndIllusion () {
            jdbcTemplate.execute("insert into tao values (1,'d')");
            try {
                Thread.sleep(00000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        //    int a = 1/0;
        }
    
        @Transactional(isolation = Isolation.REPEATABLE_READ)
        public Object listForIllusionRead() {
    
            List<Map<String,Object>> map = jdbcTemplate.queryForList("select * from tao");
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            List<Map<String,Object>> map2 = jdbcTemplate.queryForList("select * from tao");
    
            Map<String,Object> res = new HashMap<String, Object>();
            res.put("before", map);
            res.put("after", map2);
            return res;
        }
    
    
        public void updateForNoRepeat () {
            jdbcTemplate.execute("update  tao set col2 = 'e'");
        }
    
        public void deleteForNoRepeat() {
            jdbcTemplate.execute("delete from tao ");
        }
    
    }


    数据库:

    1. CREATE TABLE `tao` (  
    2.   `col1` tinyint(3) unsigned NOT NULL DEFAULT '0',  
    3.   `col2` varchar(20) DEFAULT NULL,  
    4.   PRIMARY KEY (`col1`)  
    5. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  • 相关阅读:
    java基础知识——7.断点调试与数据加密
    Hibernate最基础配置的记忆方式
    Redis集群命令
    VMware Workstation下centos的使用
    Redis入门
    Shell脚本
    使用Java遇到的问题
    使用Linux
    压缩包安装MySQL服务
    安装windows+ubuntu双系统
  • 原文地址:https://www.cnblogs.com/silyvin/p/9106776.html
Copyright © 2020-2023  润新知