gnodet-bot commented on code in PR #26845:
URL: https://github.com/apache/camel/pull/26845#discussion_r4094859731
##########
components/camel-sql/src/main/java/org/apache/camel/component/sql/SqlComponent.java:
##########
@@ -151,6 +156,23 @@ protected Endpoint createEndpoint(String uri, String
remaining, Map<String, Obje
return endpoint;
}
+ @Override
+ public void onSecretRotation(Object source) throws Exception {
+ // Use identity-based deduplication to avoid double-eviction when
this.dataSource
+ // is the same object instance as a bean registered in the registry.
+ // (equals/hashCode on DataSource wrappers may delegate to the wrapped
instance,
+ // causing a regular HashSet to miss duplicates or collapse distinct
pools.)
+ Set<DataSource> dataSources = Collections.newSetFromMap(new
IdentityHashMap<>());
+
dataSources.addAll(getCamelContext().getRegistry().findByType(DataSource.class));
+ if (this.dataSource != null) {
+ dataSources.add(this.dataSource);
+ }
+
+ for (DataSource ds : dataSources) {
+ DataSourceHelper.evictDataSourceConnections(ds, source);
+ }
+ }
Review Comment:
⚠️ **Same duplicate orchestration as `JdbcComponent.onSecretRotation()`**
This method is byte-for-byte identical to
`JdbcComponent.onSecretRotation()`. Once
`DataSourceHelper.evictAllDataSourceConnections(Registry, DataSource, Object)`
exists (see comment on the JDBC file), this becomes:
```suggestion
@Override
public void onSecretRotation(Object source) throws Exception {
DataSourceHelper.evictAllDataSourceConnections(
getCamelContext().getRegistry(), this.dataSource, source);
}
```
##########
components/camel-jdbc/src/main/java/org/apache/camel/component/jdbc/JdbcComponent.java:
##########
@@ -114,6 +114,23 @@ public void setConnectionStrategy(ConnectionStrategy
connectionStrategy) {
this.connectionStrategy = connectionStrategy;
}
+ @Override
+ public void onSecretRotation(Object source) throws Exception {
+ // Use identity-based deduplication to avoid double-eviction when
this.dataSource
+ // is the same object instance as a bean registered in the registry.
+ // (equals/hashCode on DataSource wrappers may delegate to the wrapped
instance,
+ // causing a regular HashSet to miss duplicates or collapse distinct
pools.)
+ Set<DataSource> dataSources = Collections.newSetFromMap(new
IdentityHashMap<>());
+
dataSources.addAll(getCamelContext().getRegistry().findByType(DataSource.class));
+ if (this.dataSource != null) {
+ dataSources.add(this.dataSource);
+ }
+
+ for (DataSource ds : dataSources) {
+ DataSourceHelper.evictDataSourceConnections(ds, source);
+ }
+ }
Review Comment:
⚠️ **Duplicate orchestration — still copy-pasted from `SqlComponent`**
The previous review asked to eliminate the duplicated eviction logic.
`evictDataSourceConnections()` was extracted to `DataSourceHelper` — good. But
the orchestration in `onSecretRotation()` (the IdentityHashMap setup, registry
scan, and loop) is still identical in both components, including the same
three-line comment block.
Move the orchestration into `DataSourceHelper` as well:
```java
// In DataSourceHelper:
public static void evictAllDataSourceConnections(
Registry registry, DataSource componentDataSource, Object source) {
Set<DataSource> dataSources = Collections.newSetFromMap(new
IdentityHashMap<>());
dataSources.addAll(registry.findByType(DataSource.class));
if (componentDataSource != null) {
dataSources.add(componentDataSource);
}
for (DataSource ds : dataSources) {
evictDataSourceConnections(ds, source);
}
}
```
Then both components collapse to a one-liner:
```suggestion
@Override
public void onSecretRotation(Object source) throws Exception {
DataSourceHelper.evictAllDataSourceConnections(
getCamelContext().getRegistry(), this.dataSource, source);
}
```
##########
components/camel-jdbc/src/test/java/org/apache/camel/component/jdbc/JdbcComponentSecretRotationAwareTest.java:
##########
@@ -0,0 +1,208 @@
+/*
+ * 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.jdbc;
+
+import java.io.PrintWriter;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.logging.Logger;
+
+import javax.sql.DataSource;
+
+import org.apache.camel.spi.SecretRotationAware;
+import org.apache.camel.support.DataSourceHelper;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Verifies that {@link JdbcComponent} implements {@link SecretRotationAware}
and correctly evicts stale connections on
+ * rotation.
+ */
+class JdbcComponentSecretRotationAwareTest {
+
+ @Test
+ void implementsSecretRotationAware() {
+ assertInstanceOf(SecretRotationAware.class, new JdbcComponent());
+ }
+
+ @Test
+ void evictDataSourceConnections_hikariCpPool_callsSoftEvict() throws
Exception {
+ // Arrange: a DataSource that simulates HikariDataSource by exposing
getHikariPoolMXBean(),
+ // which returns a mock MXBean with softEvictConnections(). This
matches the real HikariCP API
+ // where softEvictConnections() lives on HikariPoolMXBean, not on
HikariDataSource itself.
+ AtomicBoolean softEvictCalled = new AtomicBoolean(false);
+ Object mockMXBean = new Object() {
+ @SuppressWarnings("unused")
+ public void softEvictConnections() {
+ softEvictCalled.set(true);
+ }
+ };
+ DataSource hikariLike = new HikariLikeDataSource() {
+ @SuppressWarnings("unused")
+ public Object getHikariPoolMXBean() {
+ return mockMXBean;
+ }
+ };
+
+ // Act
+ DataSourceHelper.evictDataSourceConnections(hikariLike, "test");
+
+ // Assert
+ assertTrue(softEvictCalled.get(), "softEvictConnections() should have
been called via HikariPoolMXBean");
+ }
+
+ @Test
+ void evictDataSourceConnections_genericPool_doesNotThrow() throws
Exception {
Review Comment:
🔧 **Spurious `throws Exception`** —
`DataSourceHelper.evictDataSourceConnections()` is not declared `throws` (it
catches all exceptions internally), so this clause is dead code. The equivalent
test in `SqlComponentSecretRotationAwareTest` correctly omits it.
```suggestion
void evictDataSourceConnections_genericPool_doesNotThrow() {
```
--
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]