Croway opened a new pull request, #30184:
URL: https://github.com/apache/camel/pull/30184

   [CAMEL-27025](https://issues.apache.org/jira/browse/CAMEL-27025)
   
   ### What was wrong
   
   With the default `consumeLockEntity=true`, `JpaConsumer.lockEntity` locks 
each polled entity with `entityManager.lock(entity, lockModeType)` and no lock 
timeout; only `skipLockedEntity=true` passes a hint 
(`jakarta.persistence.lock.timeout=0`, NOWAIT). PostgreSQL has no lock timeout 
by default, so a poll waits until the transaction that holds the row lock ends. 
When that transaction does not end, for example the poll of a client whose 
connection was lost without the database noticing (PostgreSQL keeps it `idle in 
transaction` until TCP keepalive gives up, about 2 h), the consumer is stuck. A 
JDBC socket timeout ends the poll but not its wait on the database: each poll 
leaves one more session waiting for the lock (fault-injection tests with a 
silently dropped connection to a real PostgreSQL: 1 to 6 waiting sessions in 60 
s, the consumer never recovered).
   
   There was no way to bound the wait: with Hibernate 7.4 a 
`jakarta.persistence.lock.timeout` property of the persistence unit is not 
applied to `EntityManager.lock(entity, lockModeType)` (`SessionImpl.lock` 
copies only the lock options of the session), which a reproducer confirmed (the 
poll still waits).
   
   ### The fix
   
   - New consumer option `lockTimeout` (millis, a duration, default `0` = no 
timeout, so the default behaviour does not change). When set, and 
`skipLockedEntity` is not, the lock gets `jakarta.persistence.lock.timeout`. 
Hibernate 7.4 applies it on PostgreSQL with `set local lock_timeout` before the 
lock, so the database cancels the wait and no session is left waiting.
   - A "Locking the consumed entities" section in `jpa-component.adoc`: the 
lock, the case of a transaction that does not end, `lockTimeout` and 
`skipLockedEntity`, what a failed lock does (skip when the transaction can go 
on; on PostgreSQL the transaction is aborted, so the poll is rolled back and 
the entities routed before the locked one are routed again by the next poll), 
and PostgreSQL's `idle_in_transaction_session_timeout` to end the transaction 
holding the lock.
   
   ### Tests
   
   - `JpaConsumerLockTimeoutIT` (PostgreSQL from test-infra, Hibernate set up 
through Spring's `LocalContainerEntityManagerFactoryBean`): another connection 
locks the first entity and keeps its transaction open; the poll gives up after 
`lockTimeout` instead of waiting, routes nothing, and no session is left 
waiting for the lock; once that transaction ends, the next poll consumes every 
entity. Its entity is mapped in an `orm.xml` rather than annotated: the OpenJPA 
persistence units of the other tests scan the test classes and fail on an 
annotated entity that is not enhanced.
   - `JpaConsumerLockTimeoutTest`: the hint given to `EntityManager.lock` with 
`lockTimeout`, without it, and with `skipLockedEntity` (NOWAIT wins).
   - Fail before: with the option plumbed but `lockEntity` not passing the hint 
(as on main), the IT fails with `the poll waited for the lock ==> execution 
timed out after 30000 ms` and `lockTimeoutIsGivenToTheLock` fails; both pass 
with the fix.
   - camel-jpa tests pass with the default (OpenJPA) profile and with 
`-Dhibernate`.
   
   ### Design choices for reviewers
   
   - A hint on the lock, not a change of default: the default keeps waiting as 
today. No upgrade guide entry.
   - The option applies to the scheduled consumer only, like 
`skipLockedEntity`; the polling consumer (`pollEnrich`) locks through the query 
and is not changed here.
   - On PostgreSQL a failed lock aborts the transaction, so `skipLockedEntity` 
(and now `lockTimeout`) cannot skip the entity there: the poll is rolled back. 
This is existing behaviour of `skipLockedEntity`, now documented; making it 
skip would need `SKIP LOCKED` in the query or a savepoint, which is a larger 
change.
   
   _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]

Reply via email to