Croway opened a new pull request, #30209: URL: https://github.com/apache/camel/pull/30209
[CAMEL-27057](https://issues.apache.org/jira/browse/CAMEL-27057) ### What was wrong `skipLockedEntity=true` is meant to let several consumers share a table, each skipping the entities the others hold. `JpaConsumer.lockEntity` locked each entity with `EntityManager.lock(entity, lockModeType, {jakarta.persistence.lock.timeout=0})` (NOWAIT) and skipped it when the lock failed, unless the failure was a `PessimisticLockException` or an `OptimisticLockException`, which it rethrows because the transaction cannot go on. - **PostgreSQL**: a failed `FOR UPDATE NOWAIT` aborts the transaction. Hibernate reports a `PessimisticLockException` (`PostgreSQLDialect.causesRollback` is true), the final flush fails with `current transaction is aborted`, and the poll is rolled back: the entities routed before the locked one are routed again by every poll while the lock is held. - **Every database**: an entity that another consumer consumed (deleted) after the poll listed it fails the lock with an `OptimisticLockException`, which marks the transaction rollback only, so the poll is rolled back the same way (seen with Hibernate on PostgreSQL and H2, and with OpenJPA on PostgreSQL). Found with fault-injection tests against a real PostgreSQL, while working on CAMEL-27025 (#30184). ### The fix With Hibernate and a pessimistic `lockModeType`, the consumer now first selects the entity with `SKIP LOCKED`: a criteria query on the entity, with the row lock (`PESSIMISTIC_WRITE`, or `PESSIMISTIC_READ`), the hint `jakarta.persistence.lock.timeout=-2` (Hibernate's `Timeouts.SKIP_LOCKED`) and `FlushModeType.COMMIT`. On PostgreSQL Hibernate runs `select ... for no key update of ... skip locked`. No row means another transaction holds the entity, or it is gone: the consumer skips it, and the transaction is untouched. Otherwise this transaction holds the row lock, and the existing `EntityManager.lock(entity, lockModeType, NOWAIT)` then applies the lock mode without waiting (a no-op for `PESSIMISTIC_WRITE`; it increments the version for `PESSIMISTIC_FORCE_INCREMENT`). Hibernate still checks the version of a versioned entity when the query upgrades its lock, as `lock` did. Everything else keeps the NOWAIT lock: other JPA providers, the optimistic lock modes, `lockTimeout`, `skipLockedEntity=false`. The defaults do not change. ### Design choices for reviewers - **Why not `EntityManager.lock` with `-2`**: Hibernate 7.4.15 rejects it (`IllegalArgumentException: Skip-locked is not valid option for #lock`). `-2` works on queries only. - **Why only Hibernate (detected with `getDelegate()` being an `org.hibernate.Session`, loaded by name, no compile dependency)**: the JPA specification has no SKIP LOCKED. OpenJPA 4.1.1 rejects `-2` (`IllegalArgumentException` from `FetchConfigurationImpl.setLockTimeout`), and EclipseLink 5.0.2 maps only `0` to NOWAIT (`ObjectLevelReadQuery.prePrepare`) and turns other values into a wait clause, which its PostgreSQL platform does not render: a plain `FOR UPDATE` that waits. Trying `-2` everywhere and falling back on an exception would regress EclipseLink. - **Why not a savepoint around the lock**: rolling back to a savepoint would revive the PostgreSQL transaction, but Hibernate marks the session rollback only when it converts the lock failure (`ExceptionConverterImpl.rollbackIfNecessary`), so the commit rolls back anyway. - **Why not lock in the poll query**: it would change the SQL of every user query: a native query takes no lock mode, Hibernate falls back to locking the entities after the query for `DISTINCT`, `GROUP BY` or `HAVING` (follow-on locking, `AbstractSqlAstTranslator.determineLockingStrategy`), and the rows beyond `maxMessagesPerPoll` would be locked too. Locking per entity keeps the poll query and the flow as they are. - **`PESSIMISTIC_FORCE_INCREMENT`**: the select uses `PESSIMISTIC_WRITE`, because Hibernate always gives a query with `PESSIMISTIC_FORCE_INCREMENT` NOWAIT (`AbstractSqlAstTranslator.getEffectiveLockTimeout`); the `lock` call that follows increments the version. - **OpenJPA**: unchanged. It has no NOWAIT: it applies the lock timeout as the JDBC query timeout, where `0` means none, so with `skipLockedEntity=true` on PostgreSQL its lock waits for the other transaction (verified). The documentation now says so. - **Databases without SKIP LOCKED**: Hibernate then renders a plain `FOR UPDATE` (`Dialect.getForUpdateSkipLockedString`), as it does for NOWAIT (`getForUpdateNowaitString`); every core dialect of Hibernate 7.4 that overrides the NOWAIT one overrides the SKIP LOCKED one too. ### Tests - `JpaConsumerSkipLockedEntityIT` (PostgreSQL test-infra, Hibernate set up like Spring Boot, as `JpaConsumerLockTimeoutIT`; runs in both profiles): - `lockedEntityIsSkipped`: another connection holds entity 2; the poll consumes 1 and 3, the next poll skips 2 again, and after the release a poll consumes 2. Each entity is routed once. **Before**: the poll fails with `current transaction is aborted` and is rolled back. - `entityConsumedByAnotherConsumerIsSkipped`: another consumer deletes entity 2 while the poll routes entity 1. **Before**: `expected: <[]> but was: <[1, 3]>` (the poll was rolled back). - `lockedEntityIsSkippedWithPessimisticRead`: the same as the first case with `lockModeType=PESSIMISTIC_READ`. - `JpaConsumerSkipLockedEntityTest` (H2, Hibernate, both profiles): the same two cases (the first one passes before too: on H2 a failed NOWAIT does not abort the transaction; the second fails before with `expected: <[]> but was: <[1, 3]>`), plus an optimistic lock mode and another JPA provider keep the NOWAIT lock. - The camel-jpa unit tests and ITs pass in the OpenJPA (default) and Hibernate (`-Dhibernate`) profiles. ### Documentation "Locking the consumed entities" in `jpa-component.adoc` has a new "Skipping the locked entities" part; the descriptions of `skipLockedEntity` and `lockTimeout` are updated (component JSON, catalog, endpoint DSL regenerated). Upgrade guide 4.24: an entry for the change of behaviour with Hibernate. _Claude Code on behalf of Croway_ -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
