我编写了一个 JPA 存储库和一个带有版本字段的实体来支持乐观锁定。
@Version
var version: Long? = null
我编写了一个测试,模拟并发更新和删除,并且确实期望某种 OptimisticLockingException
// precondition
2 retries in database
@Transactional
fun update(id: String){
val existing = repository.findById(id)
// DELETE
// some concurrent code execution deleting the entry with id 2 in different transaction
// MODIFY list entry with id 2
repo.saveAll(existing)
}
When debugging, I can see that one entry is deleted in the database. (after the concurrent delete thread)
I would expect some kind of OptimisticLockingException on the call to save.
But JPA rather inserts the modified entry again, so that the delete is overwritten.
我可以解决这个问题。 放置 @Transactional 的方法是从同一个类内部调用的。
@Transactional 未激活