Sunday, February 7, 2016

Spring data JPA Pessimistic lock

If you want to lock a database record to execute certain business logic and do not want any other thread to update the same then do the following -

import javax.persistence.LockModeType;

public interface PersonRepository extends CrudRepository {
    @Query("select p from Person p where = :id")
    Person findOneAndLock(@Param("id") int id);
In this example Person is an entity and I want to lock a Person record based on id. The service layer method must be in transaction to use this method.

No comments:

Post a Comment