欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring 隔离级别 测试代码

程序员文章站 2022-06-14 16:40:53
...
@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