Reputation: 31
I'm trying to acquire row lock with SKIP LOCKED modifier using Spring Data JDBC.
When use JPA (Hibernate) you can achieve that by using hint @QueryHints({@QueryHint(name = "javax.persistence.lock.timeout", value="-2")})
Am I able to do this with Spring Data JDBC?
Upvotes: 2
Views: 633
Reputation: 1361
Like you mentioned, in the JPA world, the JPA implementation (e.g. Hibernate) is the one that understands javax.persistence.lock.timeout
and will (if the dialect allows), apply SKIP LOCKED
to the queries.
With Spring Data JDBC, you get the benefits of Spring Data's query derivation, but it ultimately deals with a lot less abstractions than JPA, and tends to work with native SQL more often. Which can be a challenge when it comes to things like SKIP LOCKED
, which are nonstandard and depend on the DBMS being used.
As such, there is no hint or similar concept in Spring Data JDBC that mirrors the JPA one. However, since Spring Data JDBC still allows you to specify @Query
(and only with native SQL), you could take advantage of that to use SKIP LOCKED
:
@Query("SELECT * FROM PERSON WHERE first_name = :firstName FOR UPDATE SKIP LOCKED")
List<Person> findByFirstNameForUpdateSkipLocked(String firstName);
To my knowledge, specifying SKIP LOCKED
as a native query is the only way to accomplish this with Spring Data JDBC.
Upvotes: 2