midnight
midnight

Reputation: 132

JPA repository OptimisticLockingException not thrown

I wrote a JPA repository and a entity with version field to support optimistic locking.

    @Version
    var version: Long? = null

I wrote a test, which simulates concurrent update and delete and did expect some kind of 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.









Upvotes: 0

Views: 16

Answers (1)

midnight
midnight

Reputation: 132

I could figure out the issue. The method, where the @Transactional was placed was called from inside the same class.

The @Transactional was not active

Upvotes: 0

Related Questions