我正在尝试使用Spring Data JPA(2.1)和Hibernate在PostgreSQL上执行SKIP LOCKED查询。 查询如下所示:
@Lock(LockModeType.PESSIMISTIC_WRITE)
@QueryHints({@QueryHint(name = "javax.persistence.lock.timeout", value ="-2")})
List<Obj> findByEntityAndStatus(Entity entity, Status status);
根据 Spring data JPA native query skip locked 和 Select for update skip locked from JPA level 的说法,应该可以实现skip locked,但是生成的查询语句只会选择更新而不会跳过锁定的行。
生成的查询语句如下:
我漏掉了什么?Hibernate: select obj0_.id as id1_5_, obj0_.name as name6_5_, obj0_.entity_id as entity10_5_, obj0_.status as status8_5_ from objs obj0_ left outer join entities entity1_ on obj0_.entity_id=entity1_.id where entity1_.id=? and obj0_.status=? for update of obj0_