This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 7f97a744c90d CAMEL-26987: camel-jpa - close the producer's 
EntityManager on failure, so a redelivery gets a new one (#30068)
7f97a744c90d is described below

commit 7f97a744c90d270d8caa55d4ad53c598dd7d9696
Author: Federico Mariani <[email protected]>
AuthorDate: Sun Nov 15 19:04:27 2026 +0100

    CAMEL-26987: camel-jpa - close the producer's EntityManager on failure, so 
a redelivery gets a new one (#30068)
    
    Co-authored-by: Claude <[email protected]>
---
 .../apache/camel/component/jpa/JpaProducer.java    |  37 ++-
 .../jpa/JpaProducerConnectionLossTest.java         | 272 +++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc    |  14 ++
 3 files changed, 317 insertions(+), 6 deletions(-)

diff --git 
a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
 
b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
index 81065da0724b..31131d455288 100644
--- 
a/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
+++ 
b/components/camel-jpa/src/main/java/org/apache/camel/component/jpa/JpaProducer.java
@@ -168,12 +168,37 @@ public class JpaProducer extends DefaultProducer {
         final EntityManager entityManager = getTargetEntityManager(exchange, 
entityManagerFactory,
                 getEndpoint().isUsePassedInEntityManager(), 
getEndpoint().isSharedEntityManager(), true);
 
-        if (findEntity) {
-            processFind(exchange, entityManager);
-        } else if (getQueryFactory() != null) {
-            processQuery(exchange, entityManager);
-        } else {
-            processEntity(exchange, entityManager);
+        try {
+            if (findEntity) {
+                processFind(exchange, entityManager);
+            } else if (getQueryFactory() != null) {
+                processQuery(exchange, entityManager);
+            } else {
+                processEntity(exchange, entityManager);
+            }
+        } catch (RuntimeException e) {
+            closeEntityManagerOnFailure(exchange, entityManager);
+            throw e;
+        }
+    }
+
+    /**
+     * After a failure, the EntityManager may hold a broken JDBC connection 
(Hibernate keeps it when the rollback
+     * fails), and its persistence context is undefined after the rollback 
(Jakarta Persistence, Transaction Rollback).
+     * Closes it, as JpaConsumer does, so that a redelivery of the exchange 
gets a new one from
+     * {@link JpaHelper#getTargetEntityManager}. Only the EntityManager that 
camel-jpa created for the exchange is
+     * closed: not one passed in the {@link JpaConstants#ENTITY_MANAGER} 
header, nor Spring's shared EntityManager. One
+     * that joined an outer transaction (transacted route) is closed when that 
transaction completes.
+     */
+    private static void closeEntityManagerOnFailure(Exchange exchange, 
EntityManager entityManager) {
+        Map<?, ?> entityManagers = 
exchange.getProperty(JpaConstants.ENTITY_MANAGER, Map.class);
+        if (entityManagers != null && 
entityManagers.containsValue(entityManager)) {
+            LOG.debug("Closing EntityManager {} after a failure, so that a 
redelivery uses a new one", entityManager);
+            try {
+                entityManager.close();
+            } catch (RuntimeException e) {
+                LOG.debug("Error closing EntityManager {} after a failure. 
This exception is ignored.", entityManager, e);
+            }
         }
     }
 
diff --git 
a/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerConnectionLossTest.java
 
b/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerConnectionLossTest.java
new file mode 100644
index 000000000000..eabf8fde056a
--- /dev/null
+++ 
b/components/camel-jpa/src/test/java/org/apache/camel/processor/jpa/JpaProducerConnectionLossTest.java
@@ -0,0 +1,272 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.processor.jpa;
+
+import java.io.PrintWriter;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Proxy;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.logging.Logger;
+
+import jakarta.persistence.EntityManager;
+
+import javax.sql.DataSource;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jpa.JpaConstants;
+import org.apache.camel.examples.SendEmail;
+import org.apache.camel.spi.Registry;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.h2.jdbcx.JdbcDataSource;
+import org.junit.jupiter.api.Test;
+import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The JPA producer keeps the EntityManager it creates for an exchange until 
the exchange completes, so a redelivery of
+ * the exchange used the same one. When the JDBC connection of that 
EntityManager breaks during a send (the database
+ * restarts, the network drops), the rollback fails too, and Hibernate keeps 
the broken connection in the EntityManager:
+ * every redelivery then failed although the database was back. The producer 
now closes the EntityManager it created
+ * when a send fails, so that a redelivery gets a new one.
+ * <p>
+ * Runs with the JPA provider of the maven profile in use. OpenJPA releases 
the broken connection by itself, so the
+ * redelivery only failed with Hibernate ({@code -Dhibernate}); with both, the 
redelivery must not get the EntityManager
+ * of the failed send.
+ */
+public class JpaProducerConnectionLossTest extends CamelTestSupport {
+
+    private static final String ENDPOINT_URI
+            = "jpa://" + SendEmail.class.getName() + 
"?entityManagerFactory=#entityManagerFactory";
+
+    private final List<Boolean> entityManagerOpenOnRedelivery = new 
CopyOnWriteArrayList<>();
+    private ConnectionBreakingDataSource dataSource;
+    private LocalContainerEntityManagerFactoryBean entityManagerFactory;
+
+    @Override
+    protected void doPreSetup() {
+        dataSource = new ConnectionBreakingDataSource(
+                "jdbc:h2:mem:jpa-connection-loss-" + UUID.randomUUID() + 
";DB_CLOSE_DELAY=-1");
+        // the camel persistence unit of META-INF/persistence.xml, with the 
JPA provider of the maven profile in use
+        entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
+        entityManagerFactory.setPersistenceUnitName("camel");
+        entityManagerFactory.setDataSource(dataSource);
+        entityManagerFactory.afterPropertiesSet();
+    }
+
+    @Override
+    protected void doPostTearDown() throws Exception {
+        entityManagerFactory.destroy();
+        dataSource.execute("shutdown");
+    }
+
+    @Override
+    protected void bindToRegistry(Registry registry) {
+        registry.bind("entityManagerFactory", 
entityManagerFactory.getObject());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                // the database is back right after the connections broke, so 
redeliveries without delay are enough
+                
errorHandler(defaultErrorHandler().maximumRedeliveries(3).redeliveryDelay(0)
+                        
.onRedelivery(JpaProducerConnectionLossTest.this::recordEntityManagerOnRedelivery));
+
+                from("direct:start")
+                        .to(ENDPOINT_URI);
+
+                from("direct:passedIn")
+                        .errorHandler(noErrorHandler())
+                        .to(ENDPOINT_URI + "&usePassedInEntityManager=true");
+            }
+        };
+    }
+
+    @Test
+    public void testRedeliveryAfterConnectionLoss() throws Exception {
+        dataSource.breakConnectionsOnNextInsert();
+
+        Exchange out = template.send("direct:start", e -> 
e.getIn().setBody(new SendEmail("[email protected]")));
+
+        assertEquals(1, dataSource.getBreaks(), "The connections should break 
while the entity is inserted");
+        assertNull(out.getException(), "A redelivery should persist the entity 
once the database is back");
+        assertEquals(List.of("[email protected]"), dataSource.selectAddresses());
+        assertEquals(List.of(false), entityManagerOpenOnRedelivery,
+                "The EntityManager of the failed send should be closed before 
the only redelivery, which then gets a new one");
+    }
+
+    @Test
+    public void testPassedInEntityManagerIsNotClosedOnFailure() {
+        EntityManager entityManager = 
entityManagerFactory.getObject().createEntityManager();
+        try {
+            dataSource.breakConnectionsOnNextInsert();
+
+            Exchange out = template.send("direct:passedIn", e -> {
+                e.getIn().setBody(new SendEmail("[email protected]"));
+                e.getIn().setHeader(JpaConstants.ENTITY_MANAGER, 
entityManager);
+            });
+
+            assertEquals(1, dataSource.getBreaks(), "The connections should 
break while the entity is inserted");
+            assertNotNull(out.getException(), "The exchange should fail");
+            assertTrue(entityManager.isOpen(), "The EntityManager passed in 
the header should be left to its owner");
+        } finally {
+            entityManager.close();
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    private void recordEntityManagerOnRedelivery(Exchange exchange) {
+        Map<String, EntityManager> entityManagers = 
exchange.getProperty(JpaConstants.ENTITY_MANAGER, Map.class);
+        for (EntityManager entityManager : entityManagers.values()) {
+            entityManagerOpenOnRedelivery.add(entityManager.isOpen());
+        }
+    }
+
+    /**
+     * An H2 database whose open connections all break while an entity is 
inserted, as when the database restarts or the
+     * network drops. New connections work again right away.
+     */
+    private static final class ConnectionBreakingDataSource implements 
DataSource {
+
+        private final JdbcDataSource h2 = new JdbcDataSource();
+        private final Set<Connection> open = ConcurrentHashMap.newKeySet();
+        private final AtomicBoolean breakOnNextInsert = new AtomicBoolean();
+        private final AtomicInteger breaks = new AtomicInteger();
+
+        ConnectionBreakingDataSource(String url) {
+            h2.setURL(url);
+        }
+
+        void breakConnectionsOnNextInsert() {
+            breakOnNextInsert.set(true);
+        }
+
+        int getBreaks() {
+            return breaks.get();
+        }
+
+        void execute(String sql) throws SQLException {
+            try (Connection connection = h2.getConnection(); Statement 
statement = connection.createStatement()) {
+                statement.execute(sql);
+            }
+        }
+
+        List<String> selectAddresses() throws SQLException {
+            List<String> addresses = new ArrayList<>();
+            try (Connection connection = h2.getConnection(); Statement 
statement = connection.createStatement();
+                 ResultSet rs = statement.executeQuery("select address from 
SendEmail order by id")) {
+                while (rs.next()) {
+                    addresses.add(rs.getString(1));
+                }
+            }
+            return addresses;
+        }
+
+        private void breakOpenConnections() {
+            breaks.incrementAndGet();
+            for (Connection connection : open) {
+                try {
+                    connection.close();
+                } catch (SQLException e) {
+                    // already closed
+                }
+            }
+            open.clear();
+        }
+
+        @Override
+        public Connection getConnection() throws SQLException {
+            Connection physical = h2.getConnection();
+            open.add(physical);
+            return (Connection) 
Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { 
Connection.class },
+                    (proxy, method, args) -> {
+                        if ("prepareStatement".equals(method.getName()) && 
args[0] instanceof String sql
+                                && 
sql.toLowerCase(Locale.ROOT).startsWith("insert into sendemail")
+                                && breakOnNextInsert.compareAndSet(true, 
false)) {
+                            breakOpenConnections();
+                        } else if ("close".equals(method.getName())) {
+                            open.remove(physical);
+                        }
+                        try {
+                            return method.invoke(physical, args);
+                        } catch (InvocationTargetException e) {
+                            throw e.getCause();
+                        }
+                    });
+        }
+
+        @Override
+        public Connection getConnection(String username, String password) 
throws SQLException {
+            return getConnection();
+        }
+
+        @Override
+        public PrintWriter getLogWriter() {
+            return null;
+        }
+
+        @Override
+        public void setLogWriter(PrintWriter out) {
+            // not used
+        }
+
+        @Override
+        public void setLoginTimeout(int seconds) {
+            // not used
+        }
+
+        @Override
+        public int getLoginTimeout() {
+            return 0;
+        }
+
+        @Override
+        public Logger getParentLogger() throws SQLFeatureNotSupportedException 
{
+            throw new SQLFeatureNotSupportedException();
+        }
+
+        @Override
+        public <T> T unwrap(Class<T> iface) throws SQLException {
+            throw new SQLException("Not a wrapper");
+        }
+
+        @Override
+        public boolean isWrapperFor(Class<?> iface) {
+            return false;
+        }
+    }
+}
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 050e2b1e7250..6710ec85dbe7 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -120,6 +120,20 @@ Starting in 4.24, parameter values provided via the 
`CamelJpaParameters` header
 
 Routes that previously relied on evaluating Simple expressions within the 
`CamelJpaParameters` header should evaluate those expressions in the route 
before setting the header value.
 
+=== camel-jpa - the producer closes its EntityManager when it fails
+
+When the JPA producer fails, it now closes the `EntityManager` that it created 
for the exchange, so that a redelivery
+gets a new one. Before, the exchange kept that `EntityManager` until it 
completed: with Hibernate, after the database
+connection broke during a send, the `EntityManager` kept the broken 
connection, and every redelivery failed with
+`JDBC begin transaction failed` even once the database was back. An 
`EntityManager` passed in the `CamelEntityManager`
+header (`usePassedInEntityManager=true`) and the shared `EntityManager` 
(`sharedEntityManager=true`) are not closed.
+On a transacted route, an `EntityManager` that joined the transaction is 
closed when the transaction completes.
+
+The entities that an earlier JPA step of the exchange read or saved with that 
`EntityManager` are detached after such
+a failure, as the JPA specification leaves them after a rollback. A route that 
handles the failure (for example with
+`onException` and `handled(true)`, or `doCatch`) and goes on using them can no 
longer load their lazy associations,
+and the next JPA step of the exchange uses a new `EntityManager`.
+
 === camel-openfga - writeTuples and the read operations carry a relationship 
condition
 
 `writeTuples` now honours a relationship condition set on a tuple in the 
message body: a `ClientTupleKey` built with

Reply via email to