zoukankan      html  css  js  c++  java
  • 事务隔离级别 不可重复读 spring 测试

    (一)

        @Transactional(isolation = Isolation.READ_COMMITTED)
        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'");
        }

     

    会话1执行listForIllusionRead,第一次读

    会话1sleep阻塞

    会话2执行update有效会话操作

    会话1第二次读

    会话1返回

    { "before":[ { "col1":1, "col2":"d" } ], "after":[ { "col1":1, "col2":"e" } ]}

    结论:与理论相符,两次读取不一样

    (二)

        @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'");
        }

    会话1执行listForIllusionRead,第一次读

    会话1sleep阻塞

    会话2执行update有效会话操作

    会话1第二次读

    会话1返回

    { "before":[ { "col1":1, "col2":"d" } ], "after":[ { "col1":1, "col2":"d" } ]}

    结论:

    结论:与理论相符,同一个会话的两次读取一样,为修改前的

    (三)

        @Transactional(isolation = Isolation.READ_COMMITTED)
        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执行listForIllusionRead,第一次读

    会话1sleep阻塞

    会话2执行delete有效会话操作

    会话1第二次读

    会话1返回

    { "before":[ { "col1":1, "col2":"e" } ], "after":[]}

    结论:与理论相符,两次读取不一样

    (四)

        @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执行listForIllusionRead,第一次读

    会话1sleep阻塞

    会话2执行delete有效会话操作

    会话1第二次读

    会话1返回

    { "before":[ { "col1":1, "col2":"d" } ], "after":[ { "col1":1, "col2":"d" } ]}

    结论:与理论相符,两次读取一样,为删除前的

    总结:
    (一)

                             修改方            查询方

    thread                   A                     B

    transcation          off               on(为了定义隔离级别,也为了定义为一次会话)                             

    isolation           not care       read-commited

    有重复读

     

     

     

    (二)

                             修改方            查询方

    thread                   A                     B

    transcation          off               on(为了定义隔离级别,也为了定义为一次会话)                             

    isolation           not care       repeatable_read

    无重复读

     

     

    (三)

                             修改方            查询方

    thread                   A                     B

    transcation          off               on(为了定义隔离级别,也为了定义为一次会话)                             

    isolation           not care       read-commited

    有重复读

     

     

     

    (四)

                             修改方            查询方

    thread                   A                     B

    transcation          off               on(为了定义隔离级别,也为了定义为一次会话)                             

    isolation           not care       repeatable_read



  • 相关阅读:
    (OK) CORE nodes access Internet—虚拟节点访问互联网—commands
    Open VSwitch—离开VMware的SDN之父Martin Casado是神马大神
    (OK-half) Fedora23——Docker——CORE—testing
    【codeforces 752B】Santa Claus and Keyboard Check
    【codeforces 752C】Santa Claus and Robot
    【codeforces 752E】Santa Claus and Tangerines
    【codeforces 548E】Mike and Foam
    【codeforces 752D】Santa Claus and a Palindrome
    【codeforces 752F】Santa Clauses and a Soccer Championship
    【codeforces 546A】Soldier and Bananas
  • 原文地址:https://www.cnblogs.com/silyvin/p/9106777.html
Copyright © 2011-2022 走看看