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 c37c56e3b1d3 CAMEL-27216: camel-cassandraql - the repositories no
longer close the session they are given (#30463)
c37c56e3b1d3 is described below
commit c37c56e3b1d3142f26c4475dc6c5e6daef847f2c
Author: allthingssecurity <[email protected]>
AuthorDate: Fri Nov 20 14:07:46 2026 +0530
CAMEL-27216: camel-cassandraql - the repositories no longer close the
session they are given (#30463)
CassandraSessionHolder set managedSession to false in its only constructor,
so stopping
an idempotent, aggregation or key-value repository closed the CqlSession of
the
application (used by other repositories and cql endpoints too) and set it
to null, and
the repository could not be started again. A given session is now managed
outside, and
the repositories never close it.
CassandraEndpoint keeps its session in a CassandraSessionHolder too, also
the session it
creates from its hosts. A new constructor CassandraSessionHolder(session,
managedSession)
lets the endpoint keep closing the session it creates, as before.
camel-cassandraql - the cql endpoint closes only the session it created
Review follow-up. CassandraEndpoint closed its session on stop also when the
session was given (session) or looked up (beanRef), so removing the route of
one endpoint closed a CqlSession bean that other endpoints, repositories or
the
application still used. A session created from the hosts stayed in the
session
field after the stop, so a restart of the same endpoint used the closed
session.
- CassandraEndpoint keeps a createdSession flag: it closes only the session
it
created from its hosts, and sets it back to null on stop, so the next
start
creates a new one. A given or looked-up session is held as managed
outside.
- doStop checks for a holder that a failed start did not create, and
CassandraSessionHolder.stop() does nothing on a second call.
- Javadoc on the CassandraSessionHolder one-argument constructor.
- Upgrade guide: names CassandraSessionHolder, and the endpoint change.
- CassandraRepositoriesSessionTest: an endpoint given a session or using
cql:bean does not close it; a hosts endpoint started again gets a new
session.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../component/cassandra/CassandraEndpoint.java | 17 +-
.../utils/cassandra/CassandraSessionHolder.java | 21 ++-
.../CassandraRepositoriesSessionTest.java | 171 +++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 14 ++
4 files changed, 219 insertions(+), 4 deletions(-)
diff --git
a/components/camel-cassandraql/src/main/java/org/apache/camel/component/cassandra/CassandraEndpoint.java
b/components/camel-cassandraql/src/main/java/org/apache/camel/component/cassandra/CassandraEndpoint.java
index 27f17a8eb6ed..421e5bf8ad35 100644
---
a/components/camel-cassandraql/src/main/java/org/apache/camel/component/cassandra/CassandraEndpoint.java
+++
b/components/camel-cassandraql/src/main/java/org/apache/camel/component/cassandra/CassandraEndpoint.java
@@ -79,6 +79,8 @@ public class CassandraEndpoint extends ScheduledPollEndpoint
implements Endpoint
private static final Logger LOG =
LoggerFactory.getLogger(CassandraEndpoint.class);
private volatile CassandraSessionHolder sessionHolder;
+ // whether the endpoint created the session from its hosts (and so closes
it when it stops)
+ private boolean createdSession;
@UriPath(description = "beanRef is defined using bean:id")
private String beanRef;
@@ -170,12 +172,16 @@ public class CassandraEndpoint extends
ScheduledPollEndpoint implements Endpoint
}
}
+ createdSession = false;
if (session == null && hosts != null) {
// use the session builder to create the cluster
session = createSessionBuilder().build();
+ createdSession = true;
}
- sessionHolder = new CassandraSessionHolder(session);
+ // the endpoint closes only the session it created from its hosts; a
session it was given (session) or looked
+ // up (beanRef) is managed outside, and other endpoints, repositories
or the application can use it
+ sessionHolder = new CassandraSessionHolder(session, !createdSession);
sessionHolder.start();
}
@@ -183,7 +189,14 @@ public class CassandraEndpoint extends
ScheduledPollEndpoint implements Endpoint
@Override
protected void doStop() throws Exception {
super.doStop();
- sessionHolder.stop();
+ if (sessionHolder != null) {
+ sessionHolder.stop();
+ }
+ if (createdSession) {
+ // the session is closed, so the next start creates a new one from
the hosts
+ session = null;
+ createdSession = false;
+ }
}
protected CassandraSessionHolder getSessionHolder() {
diff --git
a/components/camel-cassandraql/src/main/java/org/apache/camel/utils/cassandra/CassandraSessionHolder.java
b/components/camel-cassandraql/src/main/java/org/apache/camel/utils/cassandra/CassandraSessionHolder.java
index 6dda8217f54d..a8cf08533e1e 100644
---
a/components/camel-cassandraql/src/main/java/org/apache/camel/utils/cassandra/CassandraSessionHolder.java
+++
b/components/camel-cassandraql/src/main/java/org/apache/camel/utils/cassandra/CassandraSessionHolder.java
@@ -31,16 +31,33 @@ public class CassandraSessionHolder {
*/
private final boolean managedSession;
+ /**
+ * Holds a session that is managed outside: {@link #stop()} does not close
it. Others (such as cql endpoints, or
+ * other repositories) can use the same session, and the holder can be
started again after a stop. Use
+ * {@link #CassandraSessionHolder(CqlSession, boolean)} with {@code false}
for a session that {@link #stop()}
+ * closes.
+ *
+ * @param session the session
+ */
public CassandraSessionHolder(CqlSession session) {
+ this(session, true);
+ }
+
+ /**
+ * @param session the session
+ * @param managedSession whether the session is managed outside of the
holder; if false, {@link #stop()} closes it
+ */
+ public CassandraSessionHolder(CqlSession session, boolean managedSession) {
this.session = session;
- this.managedSession = false;
+ this.managedSession = managedSession;
}
public void start() {
}
public void stop() {
- if (!managedSession) {
+ // a second stop (such as after a start that failed) has nothing left
to close
+ if (!managedSession && session != null) {
session.close();
session = null;
}
diff --git
a/components/camel-cassandraql/src/test/java/org/apache/camel/utils/cassandra/CassandraRepositoriesSessionTest.java
b/components/camel-cassandraql/src/test/java/org/apache/camel/utils/cassandra/CassandraRepositoriesSessionTest.java
new file mode 100644
index 000000000000..accfd2b7c1a8
--- /dev/null
+++
b/components/camel-cassandraql/src/test/java/org/apache/camel/utils/cassandra/CassandraRepositoriesSessionTest.java
@@ -0,0 +1,171 @@
+/*
+ * 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.utils.cassandra;
+
+import com.datastax.oss.driver.api.core.CqlSession;
+import com.datastax.oss.driver.api.core.CqlSessionBuilder;
+import com.datastax.oss.driver.api.core.cql.PreparedStatement;
+import com.datastax.oss.driver.api.core.cql.SimpleStatement;
+import org.apache.camel.CamelContext;
+import org.apache.camel.component.cassandra.CassandraComponent;
+import org.apache.camel.component.cassandra.CassandraEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+import
org.apache.camel.processor.aggregate.cassandra.CassandraAggregationRepository;
+import
org.apache.camel.processor.idempotent.cassandra.CassandraIdempotentRepository;
+import
org.apache.camel.processor.keyvalue.cassandra.CassandraKeyValueRepository;
+import org.apache.camel.support.service.ServiceSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * The repositories use the CqlSession they are given, which other
repositories and cql endpoints can use too: stopping
+ * a repository (when its route is removed, or the CamelContext stops) must
not close that session, and the repository
+ * can be started again with it. The same holds for a cql endpoint given a
session (session or beanRef); a cql endpoint
+ * closes only the session it creates from its hosts, and creates a new one
when it is started again.
+ */
+class CassandraRepositoriesSessionTest {
+
+ @Test
+ void testIdempotentRepository() throws Exception {
+ CqlSession session = session();
+ CassandraIdempotentRepository repository = new
CassandraIdempotentRepository(session);
+ startStopStart(repository);
+ verify(session, never()).close();
+ assertSame(session, repository.getSession());
+ }
+
+ @Test
+ void testAggregationRepository() throws Exception {
+ CqlSession session = session();
+ CassandraAggregationRepository repository = new
CassandraAggregationRepository(session);
+ startStopStart(repository);
+ verify(session, never()).close();
+ assertSame(session, repository.getSession());
+ }
+
+ @Test
+ void testKeyValueRepository() throws Exception {
+ CqlSession session = session();
+ CassandraKeyValueRepository repository = new
CassandraKeyValueRepository(session);
+ startStopStart(repository);
+ verify(session, never()).close();
+ assertSame(session, repository.getSession());
+ }
+
+ @Test
+ void testEndpointClosesTheSessionItCreates() throws Exception {
+ CqlSession session = session();
+ CqlSessionBuilder builder = mock(CqlSessionBuilder.class);
+ when(builder.build()).thenReturn(session);
+ try (CamelContext context = new DefaultCamelContext()) {
+ context.start();
+ CassandraComponent component = context.getComponent("cql",
CassandraComponent.class);
+ CassandraEndpoint endpoint = new
CassandraEndpoint("cql://localhost/ks", component) {
+ @Override
+ protected CqlSessionBuilder createSessionBuilder() {
+ return builder;
+ }
+ };
+ endpoint.setHosts("localhost");
+ endpoint.start();
+ endpoint.stop();
+ }
+ // the cql endpoint created the session from its hosts, so it closes
it (unlike the session of a repository)
+ verify(session).close();
+ }
+
+ @Test
+ void testEndpointCreatesANewSessionWhenStartedAgain() throws Exception {
+ CqlSession first = session();
+ CqlSession second = session();
+ CqlSessionBuilder builder = mock(CqlSessionBuilder.class);
+ when(builder.build()).thenReturn(first, second);
+ try (CamelContext context = new DefaultCamelContext()) {
+ context.start();
+ CassandraComponent component = context.getComponent("cql",
CassandraComponent.class);
+ CassandraEndpoint endpoint = new
CassandraEndpoint("cql://localhost/ks", component) {
+ @Override
+ protected CqlSessionBuilder createSessionBuilder() {
+ return builder;
+ }
+ };
+ endpoint.setHosts("localhost");
+ endpoint.start();
+ assertSame(first, endpoint.getSession());
+ endpoint.stop();
+ verify(first).close();
+ // started again, as when its route is added again: the closed
session is not used
+ endpoint.start();
+ assertSame(second, endpoint.getSession());
+ endpoint.stop();
+ verify(second).close();
+ }
+ }
+
+ @Test
+ void testEndpointDoesNotCloseTheSessionItIsGiven() throws Exception {
+ CqlSession session = session();
+ try (CamelContext context = new DefaultCamelContext()) {
+ context.start();
+ CassandraComponent component = context.getComponent("cql",
CassandraComponent.class);
+ CassandraEndpoint endpoint = new CassandraEndpoint("cql:ks",
component);
+ endpoint.setSession(session);
+ endpoint.start();
+ endpoint.stop();
+ verify(session, never()).close();
+ // started again with the same session
+ endpoint.start();
+ assertSame(session, endpoint.getSession());
+ endpoint.stop();
+ }
+ verify(session, never()).close();
+ }
+
+ @Test
+ void testEndpointDoesNotCloseTheSessionItLooksUp() throws Exception {
+ CqlSession session = session();
+ try (CamelContext context = new DefaultCamelContext()) {
+ context.getRegistry().bind("mySession", session);
+ context.start();
+ CassandraEndpoint endpoint =
context.getEndpoint("cql:bean:mySession", CassandraEndpoint.class);
+ endpoint.start();
+ assertSame(session, endpoint.getSession());
+ endpoint.stop();
+ }
+ verify(session, never()).close();
+ }
+
+ private static void startStopStart(ServiceSupport repository) {
+ repository.start();
+ repository.stop();
+ // started again, as when its route is added again
+ repository.start();
+ repository.stop();
+ }
+
+ private static CqlSession session() {
+ CqlSession session = mock(CqlSession.class);
+
when(session.prepare(any(SimpleStatement.class))).thenReturn(mock(PreparedStatement.class));
+ return session;
+ }
+}
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 8e5d21dfb688..0305fcce72f2 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
@@ -699,6 +699,20 @@ The `cql` producer binds a `Map` body by name: each named
marker of the statemen
takes the value of the `Map` entry of that name. Before, a `Map` body was
bound as the value of a single
marker. To keep that, such as for a column of type `map`, wrap the `Map` in a
`List`.
+=== camel-cassandraql - the repositories and the cql endpoint no longer close
the session they are given
+
+`CassandraIdempotentRepository`, `CassandraAggregationRepository` and
`CassandraKeyValueRepository` (and their `Named`
+variants) no longer close the `CqlSession` they are given when they stop, such
as when their route is removed or the
+CamelContext stops. Other repositories and cql endpoints can use the same
session, and a repository that is stopped can
+be started again with it. The application closes the session when it is done
with it, as for any session it creates.
+For the same reason, `org.apache.camel.utils.cassandra.CassandraSessionHolder`
built with its one-argument constructor
+no longer closes the session in `stop()`; use the constructor
`CassandraSessionHolder(session, false)` for a session that
+`stop()` closes.
+
+A cql endpoint no longer closes a session given with `session` or `beanRef`
when it stops, such as when the route that
+uses it is removed. It still closes a session it creates from its `hosts`, and
creates a new one when it is started
+again. The application closes a session it gives to the endpoint, such as with
a destroy method of its bean.
+
=== camel-mina-sftp - checking whether a file exists takes one lstat request
The `mina-sftp` component now checks whether a remote file exists
(`fileExist=Ignore|Fail|Move`, `tempFileName`,