事务隔离级别 不可重复读 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