zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    puppeteer 模拟登录淘宝
    基于Istio构建微服务安全加固平台的探索
    试着给VuePress添加全局禁止爬取支持,基于vuepress-plugin-robots
    关于Word转Markdown的工具Typora安装及使用
    关于基于Nexus3和Docker搭建私有Nuget服务的探索
    关于Kubernetes(简称K8S)的开启及基本使用,基于Docker Desktop & WSL2
    关于WLS2中Ubuntu启用SSH远程登录功能,基于Xshell登录,支持Root
    关于Ubuntu开启ifConfig和Ping命令的支持,查看本机Ip地址和检查外部连接
    关于.Net Core使用Elasticsearch(俗称ES)、Kibana的研究说明
    关于使用Draw.io画数据库E-R图的说明
  • 原文地址:https://www.cnblogs.com/silyvin/p/9106776.html
Copyright © 2011-2022 走看看