Search code examples
jakarta-eejpalockingjta

JavaEE: 'SELECT FOR UPDATE NOWAIT' without marking the JTA transaction for rollback when fails?


I'm trying to use 'SELECT FOR UPDATE NOWAIT' in JavaEE (using JPA). This is how I do it:

props.put("javax.persistence.lock.timeout", 0);
em.find(MyEntity.class, id, LockModeType.PESSIMISTIC_READ, props);

This goes well except for one thing: If this fails (couldn't acquire the lock), then a PessimisticLockException is thrown instead of a LockTimeoutException (which is thrown if any other positive timeout is set), which causes the transaction (JTA transaction) to be marked for rollback.

I've tried using native queries instead, but it yields the same result.
I'm using Weblogic 12.1 with EclipseLink (tried TopLink as well, no difference).

Is there any way to execute a 'SELECT FOR UPDATE NOWAIT' without the JTA transaction being marked for rollback in JavaEE when the execution fails?


Solution

  • Please log a bug and vote for it.

    You can probably fix it by creating your own subclass of OraclePlatform and overriding isLockTimeoutException().

    You could also execute the query as a DatabaseQuery to avoid the rollback.

    em.unwrap(Session.class).executeQuery(((JpaQuery)query).getDatabaseQuery())