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 "); } }
数据库:
- CREATE TABLE `tao` (
- `col1` tinyint(3) unsigned NOT NULL DEFAULT '0',
- `col2` varchar(20) DEFAULT NULL,
- PRIMARY KEY (`col1`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8