How to enable LockModeType.PESSIMISTIC_WRITE when looking up entities with Spring Data JPA?

@Lock is supported on CRUD methods as of version 1.6 of Spring Data JPA (in fact, there’s already a milestone available). See this ticket for more details.

With that version you simply declare the following:

interface WidgetRepository extends Repository<Widget, Long> {

  @Lock(LockModeType.PESSIMISTIC_WRITE)
  Widget findOne(Long id);
}

This will cause the CRUD implementation part of the backing repository proxy to apply the configured LockModeType to the find(…) call on the EntityManager.

Leave a Comment