I do pessimistic locking on an entity with JPA. The db is DB2 v10.x, JPA 2.0 over Hibernate 4.x.
I don't want to wait until the lock is available (it is locked by a long-time running process, keeping the lock on the entity for several minutes). I would prefer it to fail early (saying "Somebody else is working here"), and let the user decide later if he'd like to retry or not. I'm using:
Map<String, Object> properties = new HashMap<>();
properties.put("javax.persistence.lock.timeout", 0);
getEntityManager().lock(entity, LockModeType.PESSIMISTIC_WRITE, properties);
But the timeout hint is not honored (NB: no matter how I configure it, at the Spring configuration level, at the JDBC connection level or whatever). Setting it to whatever you like never change anything.
I suspect this is because it is translated to the SQL :
select ID from FOOBAR where ID =? and VERSION =? **for read only with rs use and keep update locks**
.. and there is no "NOWAIT" clause anywhere.
Is it possible to make the timeout working as expected, maybe making JPA do a SELECT ... FOR UPDATE NOWAIT
? And additionally, if somebody know why is it translated by JPA to that statement instead of a more common one?