简体   繁体   English

Spring Data JDBC跳过锁定

[英]Spring Data JDBC skip locked

I'm trying to acquire row lock with SKIP LOCKED modifier using Spring Data JDBC.我正在尝试使用 Spring Data JDBC 获取带有 SKIP LOCKED 修饰符的行锁。

When use JPA (Hibernate) you can achieve that by using hint @QueryHints({@QueryHint(name = "javax.persistence.lock.timeout", value="-2")})使用 JPA (Hibernate) 时,您可以通过使用提示@QueryHints({@QueryHint(name = "javax.persistence.lock.timeout", value="-2")})来实现

Am I able to do this with Spring Data JDBC?我可以用 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.就像您提到的,在 JPA 世界中,JPA 实现(例如 Hibernate)是理解javax.persistence.lock.timeout并且将(如果方言允许)将SKIP LOCKED应用于查询的实现。

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.使用 Spring Data JDBC,您可以获得 Spring Data 查询派生的好处,但它最终处理的抽象比 JPA 少得多,并且倾向于更频繁地使用本机 SQL。 Which can be a challenge when it comes to things like SKIP LOCKED , which are nonstandard and depend on the DBMS being used.当涉及到诸如SKIP LOCKED之类的东西时,这可能是一个挑战,它们是非标准的并且取决于所使用的 DBMS。

As such, there is no hint or similar concept in Spring Data JDBC that mirrors the JPA one.因此,Spring Data JDBC 中没有提示或类似概念反映 JPA 之一。 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 :但是,由于 Spring Data JDBC 仍然允许您指定@Query (并且仅使用本机 SQL),您可以利用它来使用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.据我所知,将SKIP LOCKED指定为本机查询是使用 Spring Data JDBC 完成此任务的唯一方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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