Hibernate跳过锁定行

Hibernate跳过锁定行

本文介绍了Spring Data JPA + Hibernate跳过锁定行(PostgreSQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Spring Data JPA(2.1)和Hibernate在PostgreSQL上执行SKIP LOCKED查询.查询看起来像这样:

I am trying to execute SKIP LOCKED query on PostgreSQL using Spring Data JPA (2.1) and Hibernate. The query looks like this:

@Lock(LockModeType.PESSIMISTIC_WRITE)
@QueryHints({@QueryHint(name = "javax.persistence.lock.timeout", value ="-2")})
List<Obj> findByEntityAndStatus(Entity entity, Status status);

根据 Spring data JPA本机查询跳过锁定选择从JPA级别锁定的更新跳过,它应该可以工作,但是生成的查询仅选择要更新,而不会跳过锁定的行.

According to Spring data JPA native query skip locked and Select for update skip locked from JPA level it should work but the generated query only selects for update without skipping locked rows.

生成的查询:

我想念什么?

推荐答案

您可以使用nativeQuery.

You can use nativeQuery.

@Query(value = "SELECT * FROM task LIMIT 10 FOR UPDATE SKIP LOCKED", nativeQuery = true)
List<TaskEntity> fetchAllUnlocked();

这篇关于Spring Data JPA + Hibernate跳过锁定行(PostgreSQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 21:26