简体   繁体   中英

Spring Data JDBC skip locked

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?

Like you mentioned, in the JPA world, the JPA implementation (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM