I want to execute a SKIP LOCKED
query on Oracle using Spring Data JPA, so I tried the following:
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query(value = "SELECT * FROM User WHERE ID=?1 FOR UPDATE SKIP LOCKED", nativeQuery = true)
User findOne(UUID id);
I tried the above and found that the generated query contains FOR UPDATE
, but not SKIP LOCKED
(below is the generated query from logs):
select ent0_.column1 as name, ent0_.CREATED_DATE as CREATED_2_33_0_ from TABLE_NAME alias_name where ent0_.column1=? for update
If I remove @Lock
from the query method, the generated query does not even have FOR UPDATE
.
Please suggest how I can generate a query with FOR UPDATE SKIP LOCKED
, as required.
Add:
You can assing
-2
to timeout value so that 'skip locked' will be used whenever possible.By doing like this, the select query will have
select ... for update skip locked
https://docs.jboss.org/hibernate/orm/5.0/userguide/html_single/chapters/locking/Locking.html