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

   # Description
   
   [CAMEL-27284](https://issues.apache.org/jira/browse/CAMEL-27284)
   
   CAMEL-24132 (M4) made `JpaMessageIdRepository.add` return `false` when its 
insert fails on the unique key because another caller (node, thread) inserted 
the same message id at the same time, which is what an idempotent repository 
shared by a cluster has to handle. It recognises the failure only as 
`SQLIntegrityConstraintViolationException` or `EntityExistsException` in the 
cause chain, inside the transaction:
   
   - The PostgreSQL driver (`PSQLException`, SQLState 23505) and the SQL Server 
driver (SQLState 23000) throw a plain `SQLException`, and Hibernate throws its 
own `ConstraintViolationException` (a `PersistenceException`) with it as the 
cause (checked in `ExceptionConverterImpl` of Hibernate 7.4: only 
`NonUniqueObjectException` / `PersistentObjectException` become 
`EntityExistsException`, a `JDBCException` is rethrown as it is after marking 
the transaction for rollback). So on these databases `add` throws 
`PersistenceException("Something went wrong ...")`, and the duplicate message 
fails (error handler, redelivery, dead letter channel, `.error` folder of a 
file consumer) instead of being filtered out. H2, Derby, MySQL and Oracle throw 
the subclass, which is why the module tests pass.
   - When the EntityManager is the transaction's (`sharedEntityManager=true`), 
the failed flush marks the transaction rollback-only, so the commit throws 
after the catch returned `false`, and `add` fails on any database.
   
   Fix: the SQLState class 23 counts as a constraint violation, as 
`JpaKeyValueRepository` (CAMEL-26736) already does, and a duplicate recognised 
in the transaction stays a duplicate when the commit then fails (the check also 
runs on the exception of the transaction, outside it). Any other failure is 
thrown as before.
   
   Model: a small TLA+ model (`JpaIdemAdd.tla`: two callers add the same key; 
the driver reports the unique violation as the subclass or by SQLState; shared 
or own EntityManager) checks that exactly one caller adds the key and nobody 
fails. On main TLC finds the PostgreSQL case and the shared EntityManager case; 
the H2 case holds; with the change all hold (20 and 28 distinct states).
   
   Tests: `JpaMessageIdRepositoryConcurrentInsertTest` (Mockito, as the query 
finds no row because the other caller has not committed yet): the flush throws 
Hibernate's `ConstraintViolationException` for a PostgreSQL (23505) and a SQL 
Server (23000) unique violation, for an H2-style 
`SQLIntegrityConstraintViolationException` (control), and with a transaction 
whose commit then fails: `add` must return `false`; a connection failure 
(08006), a commit failure without a duplicate, and a new message id behave as 
before. On main the PostgreSQL, SQL Server and failed-commit tests fail; the 
other four are controls. With the change all seven pass, and the camel-jpa 
idempotent repository and consumer tests pass (11 run, 0 failures, with 
Hibernate 7.4.12 as 7.4.10 is not in my local repository). The new class 20 
times in one JVM: 140 run, 0 failures.
   
   Not changed: `remove`, `contains` and `clear` (no insert); inside a caller's 
transaction the failed insert still marks that transaction for rollback, as 
`JpaKeyValueRepository` documents.
   
   # Target
   
   - [x] I checked that the commit is targeting the correct branch (Camel 4 
uses the `main` branch)
   
   # Tracking
   - [x] If this is a large change, bug fix, or code improvement, I checked 
there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for 
the change (usually before you start working on it): 
[CAMEL-27284](https://issues.apache.org/jira/browse/CAMEL-27284).
   
   # Apache Camel coding standards and style
   
   - [x] I checked that each commit in the pull request has a meaningful 
subject line and body.
   - [ ] I have run `mvn clean install -DskipTests` locally from root folder 
and I have committed all auto-generated changes.
     (I built the touched modules with the formatter and import-sort plugins. I 
did not run the full root build.)
   
   # AI-assisted contributions
   
   - [x] If this PR includes AI-generated code, commits have proper 
co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR 
description identifies the AI tool used.
     This PR was prepared with Claude Code (Claude Opus 5.5). The commit 
carries a `Co-Authored-By` trailer.
   
   _Claude Code on behalf of allthingssecurity_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
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