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

Croway 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 39ba5e7a05ed CAMEL-27021: camel-core - transacted makes a local 
transaction manager only when the registry has none, and fails with two or more
39ba5e7a05ed is described below

commit 39ba5e7a05ed9ecbd51a672cea247c4a58221073
Author: croway <[email protected]>
AuthorDate: Mon Nov 16 10:01:48 2026 +0100

    CAMEL-27021: camel-core - transacted makes a local transaction manager only 
when the registry has none, and fails with two or more
    
    transacted() without a policy made a local JdbcTransactionManager whenever 
findSingleByType(PlatformTransactionManager)
    returned null, which is also the case with two or more transaction 
managers. With one DataSource the route then started
    and used a new transaction manager instead of one of the application's; 
with two DataSources it failed saying that no
    PlatformTransactionManager could be found.
    
    The local transaction manager (CAMEL-26851) is now made only when the 
registry has no transaction manager. With two or
    more, the route fails to start with a NoSuchBeanTypeException that says how 
many were found. The transactional client
    page says to refer to a SpringTransactionPolicy in that case.
---
 .../camel/catalog/docs/transactional-client.adoc   |   5 +
 .../SqlTransactedTwoTransactionManagersTest.java   | 175 +++++++++++++++++++++
 .../modules/eips/pages/transactional-client.adoc   |   5 +
 .../camel/reifier/AbstractPolicyReifier.java       |  14 +-
 4 files changed, 196 insertions(+), 3 deletions(-)

diff --git 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/transactional-client.adoc
 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/transactional-client.adoc
index 527f615754f1..741ae6cad1bd 100644
--- 
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/transactional-client.adoc
+++ 
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/transactional-client.adoc
@@ -195,6 +195,11 @@ xref:ROOT:sql-component.adoc[SQL] and 
xref:ROOT:spring-jdbc-component.adoc[Sprin
 the transaction, as they get their connections through Spring. The 
xref:ROOT:jdbc-component.adoc[JDBC] component
 takes its connections from the `DataSource` directly, so it does not.
 
+With two or more transaction managers, a route with `transacted` and no 
transaction policy fails to start, as Camel
+does not choose one of them (in a Spring application context, it uses the 
`@Primary` one). Refer to a
+`SpringTransactionPolicy` that has the transaction manager to use, as with 
`transacted("txRequired")` in the next
+section.
+
 === Using different transaction propagations
 
 In some rare situations, you may need to use multiple transactions with the 
same exchange.
diff --git 
a/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlTransactedTwoTransactionManagersTest.java
 
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlTransactedTwoTransactionManagersTest.java
new file mode 100644
index 000000000000..6e1ab0042c32
--- /dev/null
+++ 
b/components/camel-sql/src/test/java/org/apache/camel/component/sql/SqlTransactedTwoTransactionManagersTest.java
@@ -0,0 +1,175 @@
+/*
+ * 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.component.sql;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import javax.sql.DataSource;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.FailedToCreateRouteException;
+import org.apache.camel.NoSuchBeanTypeException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.Registry;
+import org.apache.camel.spring.spi.SpringTransactionPolicy;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
+import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.TransactionDefinition;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * CAMEL-27021: transacted without a policy uses the transaction manager of 
the registry when there is exactly one, and
+ * makes a local one for the DataSource only when there is none (CAMEL-26851). 
With two or more, the route does not
+ * start.
+ */
+public class SqlTransactedTwoTransactionManagersTest extends CamelTestSupport {
+
+    private static final String INSERT = "sql:insert into customer 
values('cust1','cmueller')?dataSource=#testdb";
+
+    private EmbeddedDatabase db;
+    private JdbcTemplate jdbc;
+
+    /**
+     * Counts the transactions it begins.
+     */
+    static class CountingTransactionManager extends 
DataSourceTransactionManager {
+        final AtomicInteger begins = new AtomicInteger();
+
+        CountingTransactionManager(DataSource dataSource) {
+            super(dataSource);
+        }
+
+        @Override
+        protected void doBegin(Object transaction, TransactionDefinition 
definition) {
+            begins.incrementAndGet();
+            super.doBegin(transaction, definition);
+        }
+    }
+
+    @Override
+    protected void bindToRegistry(Registry registry) {
+        db = new EmbeddedDatabaseBuilder()
+                .setName(getClass().getSimpleName())
+                .setType(EmbeddedDatabaseType.H2).build();
+        registry.bind("testdb", db);
+    }
+
+    @Override
+    public void doPostSetup() {
+        jdbc = new JdbcTemplate(db);
+        jdbc.execute("CREATE TABLE CUSTOMER (ID VARCHAR(15) NOT NULL PRIMARY 
KEY, NAME VARCHAR(100))");
+    }
+
+    @Override
+    public void doPostTearDown() {
+        if (db != null) {
+            db.shutdown();
+        }
+    }
+
+    private void addInsertThenFailRoute() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start").routeId("insert")
+                        .transacted()
+                        .to(INSERT)
+                        .throwException(new IllegalStateException("Forced"));
+            }
+        });
+    }
+
+    private void assertInsertRolledBack() {
+        Exchange out = template.send("direct:start", e -> 
e.getIn().setBody("x"));
+        assertTrue(out.isFailed());
+        assertEquals(0, jdbc.queryForObject("select count(*) from customer", 
Long.class));
+    }
+
+    private void assertFailsWithTwoTransactionManagers() {
+        FailedToCreateRouteException e = 
assertThrows(FailedToCreateRouteException.class, this::addInsertThenFailRoute);
+        NoSuchBeanTypeException cause = 
assertInstanceOf(NoSuchBeanTypeException.class, e.getCause());
+        assertTrue(cause.getMessage().startsWith(
+                "Found 2 beans for: " + 
PlatformTransactionManager.class.getName() + " in the registry"),
+                cause.getMessage());
+        // no local transaction manager is made
+        assertNull(context.getRegistry().lookupByName("transactionManager"));
+        assertEquals(2, 
context.getRegistry().findByType(PlatformTransactionManager.class).size());
+    }
+
+    @Test
+    public void twoTransactionManagersFailTheRoute() {
+        context.getRegistry().bind("txA", new CountingTransactionManager(db));
+        context.getRegistry().bind("txB", new CountingTransactionManager(db));
+
+        assertFailsWithTwoTransactionManagers();
+    }
+
+    @Test
+    public void twoTransactionManagersAndTwoDataSourcesFailTheRoute() {
+        EmbeddedDatabase db2 = new EmbeddedDatabaseBuilder()
+                .setName(getClass().getSimpleName() + "2")
+                .setType(EmbeddedDatabaseType.H2).build();
+        try {
+            context.getRegistry().bind("testdb2", db2);
+            context.getRegistry().bind("txA", new 
CountingTransactionManager(db));
+            context.getRegistry().bind("txB", new 
CountingTransactionManager(db2));
+
+            assertFailsWithTwoTransactionManagers();
+        } finally {
+            db2.shutdown();
+        }
+    }
+
+    @Test
+    public void twoTransactionManagersAndAPolicyUseThePolicy() throws 
Exception {
+        CountingTransactionManager txA = new CountingTransactionManager(db);
+        CountingTransactionManager txB = new CountingTransactionManager(db);
+        context.getRegistry().bind("txA", txA);
+        context.getRegistry().bind("txB", txB);
+        context.getRegistry().bind("PROPAGATION_REQUIRED", new 
SpringTransactionPolicy(txB));
+
+        addInsertThenFailRoute();
+
+        assertInsertRolledBack();
+        assertEquals(0, txA.begins.get());
+        assertEquals(1, txB.begins.get());
+        assertNull(context.getRegistry().lookupByName("transactionManager"));
+    }
+
+    @Test
+    public void oneTransactionManagerIsUsed() throws Exception {
+        CountingTransactionManager txA = new CountingTransactionManager(db);
+        context.getRegistry().bind("txA", txA);
+
+        addInsertThenFailRoute();
+
+        assertInsertRolledBack();
+        assertEquals(1, txA.begins.get());
+        assertNull(context.getRegistry().lookupByName("transactionManager"));
+    }
+}
diff --git 
a/core/camel-core-engine/src/main/docs/modules/eips/pages/transactional-client.adoc
 
b/core/camel-core-engine/src/main/docs/modules/eips/pages/transactional-client.adoc
index 527f615754f1..741ae6cad1bd 100644
--- 
a/core/camel-core-engine/src/main/docs/modules/eips/pages/transactional-client.adoc
+++ 
b/core/camel-core-engine/src/main/docs/modules/eips/pages/transactional-client.adoc
@@ -195,6 +195,11 @@ xref:ROOT:sql-component.adoc[SQL] and 
xref:ROOT:spring-jdbc-component.adoc[Sprin
 the transaction, as they get their connections through Spring. The 
xref:ROOT:jdbc-component.adoc[JDBC] component
 takes its connections from the `DataSource` directly, so it does not.
 
+With two or more transaction managers, a route with `transacted` and no 
transaction policy fails to start, as Camel
+does not choose one of them (in a Spring application context, it uses the 
`@Primary` one). Refer to a
+`SpringTransactionPolicy` that has the transaction manager to use, as with 
`transacted("txRequired")` in the next
+section.
+
 === Using different transaction propagations
 
 In some rare situations, you may need to use multiple transactions with the 
same exchange.
diff --git 
a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AbstractPolicyReifier.java
 
b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AbstractPolicyReifier.java
index 6680be8a5011..df3f83fa237e 100644
--- 
a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AbstractPolicyReifier.java
+++ 
b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AbstractPolicyReifier.java
@@ -20,6 +20,7 @@ import java.lang.reflect.Method;
 import java.util.Set;
 
 import org.apache.camel.CamelContext;
+import org.apache.camel.NoSuchBeanTypeException;
 import org.apache.camel.Route;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.model.ProcessorDefinition;
@@ -82,9 +83,16 @@ public abstract class AbstractPolicyReifier<T extends 
ProcessorDefinition<?>> ex
                 // registry
                 Object transactionManager = findSingleByType(tmClazz);
                 if (transactionManager == null) {
-                    // none, but the application has one DataSource (Camel 
Main, camel run): a local transaction
-                    // manager for it
-                    transactionManager = localTransactionManager(tmClazz);
+                    int found = findByType(tmClazz).size();
+                    if (found > 1) {
+                        // Camel does not choose one of them: the route must 
refer to a policy with the one to use
+                        throw new NoSuchBeanTypeException(tmClazz, found);
+                    }
+                    if (found == 0) {
+                        // none, but the application has one DataSource (Camel 
Main, camel run): a local transaction
+                        // manager for it
+                        transactionManager = localTransactionManager(tmClazz);
+                    }
                 }
                 if (transactionManager == null) {
                     // fails telling that there is no 
PlatformTransactionManager

Reply via email to