如何解决spring data r2dbc 使用@Transaction 有一个缓存
spring boot 数据r2dbc时,我使用Transaction注解打开Transaction,代码遇到OptimisticLockingFailureException就去查询数据库并再次更新。
但是我发现每次数据都是一样的,即使数据库变了,去掉Transaction注解后这个问题就消失了
我使用的数据库是 MySQL。
spring boot 版本:2.5.0
数据库驱动:dev.miku:r2dbc-mysql.0.8.2.RELEASE
我能做些什么来解决这个问题吗?
欢迎任何答案。
让我添加一些代码示例:
数据库实体:
@Table("test")
class Test {
@Id
private Integer id;
private String name;
@Version
private Integer version;
}
存储库:
@Repository
interface TestRepository extends R2dbcRepository<Test,Integer> {
}
服务:
@Transactional
public Mono<Boolean> update(Integer id,String name) {
return testRepository.findById(id)
.doOnNext(System.out::println)
.flatMap(test -> {
//Debug breakpoints here,And the value of the database version field is changed. Simulate the situation where intermediate data is modified
test.setName(name);
return testRepository.save(test);
})
.retryWhen(Retry.fixedDelay(3,Duration.ofMillis(100))
.filter(e -> e instanceof OptimisticLockingFailureException)
)
.map(ignore -> true);
}
当停在断点处修改数据库模拟并发变化时,可以看到查询SQL又执行了两次,但是数据是一样的,没有变化。
删除@Transactional 后,这种情况就会消失。
你也可以在 gitHub 上讨论: https://github.com/spring-projects/spring-data-r2dbc/issues/626
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。