问题描述
我对@Transactional(也称为spring-tx)有疑问。
在这样的代码中。
@Transactional(propagation = Propagation.REQUIRES_NEW,isolation = Isolation.SERIALIZABLE)
public TestObject testTransaction(Long id,String name,String content) {
//This is find
TestObject testObject = testObjectRepository.findById(id).orElse(null);
if (testObject != null) {
System.out.println(testObject.getId() + "/"
+ testObject.getName() + "/"
+ testObject.getContent());
}
// update something
testObject.changeContent(name,content);
slowQuery();
//delay for late
return testObject;
}
如果我使用邮递员请求类似的内容。
第一个请求是http://localhost:8080/spring/test/transaction?id=1&name=123&content=123
第二个请求是http://localhost:8080/spring/test/transaction?id=1&name=456&content=456
然后我想,第一个请求挂在事务上,然后第二个请求必须被Spring-tx拒绝,因为第一个请求事务的隔离级别为SERIALIZABLE
,并且事务工作未完成。
但是两个请求的结果是
{
"name":"456","content":"456"
}
有人知道事务隔离吗,我将mysql与docker结合使用,而且spring-data-jpa也是如此。
解决方法
如果您使用@Transactional批注,则要确保在db中进行了提交,请再使用一个批注,因为@EnableTransactionManagement还包括以下代码
connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
有关相同内容的更多详细信息,请遵循此link