import org.springframework.data.jpa.repository.Lock; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; import javax.persistence.LockModeType; public interface PersonRepository extends CrudRepositoryIn 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.{ @Lock(LockModeType.PESSIMISTIC_WRITE) @Query("select p from Person p where p.id = :id") Person findOneAndLock(@Param("id") int id); }
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 -
Subscribe to:
Post Comments (Atom)
How we can test above case by junit?
ReplyDelete@Nitin One option is write tests integrated with database as essential you want to test if all the annotations used are matching with database schema. Use two threads in which first one takes the lock and wait. Second thread then tries and it will fail so assert for the exception.
ReplyDeleteCan this be applied to views? if so how? I am using Couchbase db
ReplyDeleteThanks and that i have a nifty proposal: Where To Start Renovating House home improvement near me
ReplyDelete