我只是想用spring spring jpa和spring boot测试Spring的Transactional注释支持。
这是我的控制器方法。
@GetMapping(path = "testreaduncommit")
public void testReadUnCommit()
{
new Thread(new Runnable() {
@Override
public void run() {
testService.testReadUncommitted1();
}
}).start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
testService.testReadUncommitted2();
}
还有我的服务工具
@Override
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void testReadUncommitted1() {
log.info("testUncommit1 begin");
User u=userRepository.findOne(1L);
u.setUserPwd("654321");
userRepository.save(u);
log.info("testUncommit1 sleep");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("testUncommit1 will be commit");
}
@Override
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void testReadUncommitted2() {
log.info("testUncommit2 begin");
User u=userRepository.findOne(1L);
log.info(u);
}
结果是:
testUncommit1开始;
testUncommit1睡眠;
testUncommit2开始;
用户(userId = 1,userName = testuser,userPhone = 15888888888,userStatus = 2,userPwd = 123456);
testUncommit1将提交;
所以我想知道为什么注释@Transactional的属性'isolation = Isolation.READ_UNCOMMITTED'不起作用,我认为结果应该是userPwd = 654321,因为READ_UNCOMMITTED应该有'脏读'。
我试图添加userRepository.flush();
,它已经影响但是在我将隔离级别更改为SERIALIZABLE后,脏读也发生了,为什么?
我的数据库是Mysql。
所以当我添加flush(); 我改变了方法的隔离度;
@Override
@Transactional(isolation = Isolation.SERIALIZABLE)
public void testReadCommit1() {
log.info("testReadCommit1 begin");
User u=userRepository.findOne(1L);
u.setUserPwd("654321");
userRepository.save(u);
userRepository.flush();
log.info("testReadCommit1 sleep");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("testReadCommit1 will be commit");
}
@Override
@Transactional(isolation = Isolation.SERIALIZABLE)
public void testReadCommit2() {
log.info("testUncommit2 begin");
User u=userRepository.findOne(1L);
log.info(u);
}
结果是:
用户(userId = 1,userName = testuser,userPhone = 15888888888,userStatus = 2,userPwd = 654321)
为什么SERIALIZABLE没有阻止脏读?
答案 0 :(得分:0)
以下API文档说,调用flush()会将所有挂起的更改刷新到数据库。因此,当你的testReadCommit2()方法在3秒后运行时,它不会读取脏记录。