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

   JIRA: https://issues.apache.org/jira/browse/CAMEL-26987
   
   ## Problem
   
   The JPA producer keeps the `EntityManager` it creates for an exchange 
(stored in the `CamelEntityManager` exchange property by 
`JpaHelper.createEntityManager`) until the exchange completes, so every 
redelivery of the exchange reuses it. `JpaHelper.getTargetEntityManager` 
creates a new one only when that one is closed (CAMEL-9177).
   
   When the JDBC connection of that `EntityManager` breaks during a send 
(database restart, failover, network drop), the rollback fails too, and 
Hibernate keeps the broken connection in the `EntityManager`. Every redelivery 
then fails with `org.hibernate.TransactionException: JDBC begin transaction 
failed`, even after the database is back, until the redeliveries are exhausted. 
This happens with Spring's `HibernateJpaVendorAdapter` (as in Spring Boot) and 
with Hibernate's default connection handling. OpenJPA releases the broken 
connection by itself, so its redeliveries succeed.
   
   `JpaConsumer` already closes its `EntityManager` when a poll fails 
(CAMEL-11197), but the producer does not.
   
   ## Fix
   
   In `JpaProducer.process`, when the find, query or entity processing throws, 
the producer closes the `EntityManager` if camel-jpa created it for this 
exchange (it is a value of the `CamelEntityManager` exchange property map). 
Exceptions from `close()` are ignored. On the redelivery, the existing 
`!em.isOpen()` check in `getTargetEntityManager` creates a new one.
   
   - An `EntityManager` passed in the `CamelEntityManager` header 
(`usePassedInEntityManager=true`) is never in that map, so it is left alone.
   - Spring's shared `EntityManager` (`sharedEntityManager=true`) is not in the 
map either.
   - On a transacted route, an `EntityManager` that joined the transaction is 
closed when that transaction completes (Spring's application-managed 
`EntityManager` defers `close()` until then), so the transaction is not cut 
short.
   
   Side effect, in the upgrade guide: after a failure in the producer, entities 
that an earlier JPA step of the same exchange read or saved with that 
`EntityManager` are detached. The JPA specification already leaves them 
undefined after a rollback.
   
   ## Tests
   
   `JpaProducerConnectionLossTest` uses the `camel` persistence unit with the 
JPA provider of the active Maven profile (OpenJPA by default, Hibernate with 
`-Dhibernate`) and a `DataSource` that closes all open connections while the 
first entity insert is sent, as a database restart does:
   
   - `testRedeliveryAfterConnectionLoss`: the entity is persisted on a 
redelivery, and the `EntityManager` of the failed send is closed before the 
redelivery runs. Without the fix, it fails with Hibernate (`JDBC begin 
transaction failed` on all 3 redeliveries) and with OpenJPA (the redelivery 
reuses the open `EntityManager`).
   - `testPassedInEntityManagerIsNotClosedOnFailure`: an `EntityManager` passed 
in the header stays open after a failure.
   
   The whole camel-jpa suite passes with the default profile (OpenJPA) and with 
`-Dhibernate`.
   
   _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