Skip to content

Explore inheritance of @LockMode when defined on default interface methods #4217

@ilia1243

Description

@ilia1243

Let's assume we need a CrudRepository with two methods:

  • findById with default behaviour
  • findByIdForUpdate with LockModeType.PESSIMISTIC_WRITE

Naive way to implement it might be:

@Repository
interface MyRepository : CrudRepository<MyEntity, Long> {
    @Lock(LockModeType.PESSIMISTIC_WRITE)
    @Query("from MyEntity where id = :id")
    fun findByIdForUpdate(id: Long): Optional<MyEntity>
}

However, it produces two queries if called two times in the same transaction.

This can be avoided using Custom Repository Implementations https://docs.spring.io/spring-data/jpa/reference/repositories/custom-implementations.html and EntityManager.find(Class, Object, LockModeType).

What do you think about an out-of-box method such as CrudRepository.findById(ID, LockModeType) to support this?

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions