gnodet-bot commented on code in PR #29629:
URL: https://github.com/apache/camel/pull/29629#discussion_r4538221291
##########
core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AbstractPolicyReifier.java:
##########
@@ -119,4 +129,34 @@ public Policy resolvePolicy(Policy policy, String ref,
Class<? extends Policy> t
return answer;
}
+
+ /**
+ * A local transaction manager for the one DataSource of the registry,
when the registry has no transaction manager,
+ * as with Camel Main and camel run (CAMEL-26851). It is bound as
transactionManager, so the other transacted routes
+ * use it too. Null without exactly one DataSource, or without spring-jdbc
on the classpath.
+ */
+ private Object localTransactionManager(Class<?> tmClazz) {
+ Class<?> dsClazz =
camelContext.getClassResolver().resolveClass("javax.sql.DataSource");
+ Class<?> jtmClazz = camelContext.getClassResolver()
+
.resolveClass("org.springframework.jdbc.support.JdbcTransactionManager");
+ if (dsClazz == null || jtmClazz == null ||
!tmClazz.isAssignableFrom(jtmClazz)) {
+ return null;
+ }
+ Set<?> dataSources = camelContext.getRegistry().findByType(dsClazz);
+ if (dataSources.size() != 1) {
+ return null;
+ }
+ Object dataSource = dataSources.iterator().next();
+ Object transactionManager;
+ try {
+ transactionManager =
jtmClazz.getConstructor(dsClazz).newInstance(dataSource);
+ } catch (Exception e) {
+ throw new RuntimeCamelException("Cannot create a
JdbcTransactionManager for the DataSource: " + dataSource, e);
+ }
+ if (camelContext.getRegistry().lookupByName("transactionManager") ==
null) {
+ camelContext.getRegistry().bind("transactionManager",
transactionManager);
+ }
+ LOG.info("Using a local JdbcTransactionManager for transacted routes,
with the DataSource: {}", dataSource);
Review Comment:
đź’ˇ **Still open from previous review:** The `LOG.info` fires unconditionally
after the `if`-block guard. When a `"transactionManager"` bean already exists
under that name and the `bind` is skipped, the message *"Using a local
JdbcTransactionManager…"* still logs — but the returned TM is whatever was just
constructed, not what the registry holds. The log is misleading in that case.
Move the log inside the `if`-block:
```suggestion
if (camelContext.getRegistry().lookupByName("transactionManager") ==
null) {
camelContext.getRegistry().bind("transactionManager",
transactionManager);
LOG.info("Using a local JdbcTransactionManager for transacted
routes, with the DataSource: {}", dataSource);
}
```
--
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]