Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,22 @@ private static String readToSessionHash(Read<?> read) {
}

static Session getSession(Read<?> read) {
String clusterHash = readToClusterHash(read);
String sessionHash = readToSessionHash(read);

Cluster cachedCluster = clusterMap.get(clusterHash);

if (cachedCluster != null && cachedCluster.isClosed()) {
Session brokenSession = sessionMap.get(sessionHash);
if (brokenSession != null) {
sessionMap.remove(sessionHash, brokenSession);
}
// Removing broken cluster object
clusterMap.remove(clusterHash, cachedCluster);
}
Cluster cluster =
clusterMap.computeIfAbsent(
readToClusterHash(read),
clusterHash,
k ->
CassandraIO.getCluster(
Objects.requireNonNull(read.hosts()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;

import com.datastax.driver.core.Cluster;
Expand Down Expand Up @@ -1218,4 +1221,31 @@ public int hashCode() {
return Objects.hashCode(tableColumn, indexColumn, valueColumn, data);
}
}

@Test
public void testSessionEvictionOnClosedCluster() {
CassandraIO.Read<String> readConfig =
CassandraIO.<String>read()
.withHosts(Collections.singletonList(CASSANDRA_HOST))
.withPort(cassandraPort)
.withKeyspace(CASSANDRA_KEYSPACE)
.withTable(CASSANDRA_TABLE);

Session initialSession = ConnectionManager.getSession(readConfig);
Cluster initialCluster = initialSession.getCluster();

initialCluster.close();
assertTrue("Cluster should be closed", initialCluster.isClosed());

Session newSession = ConnectionManager.getSession(readConfig);
Cluster newCluster = newSession.getCluster();

assertNotNull("New session should not be null", newSession);
assertFalse("New cluster should be open", newCluster.isClosed());

assertNotSame(
"ConnectionManager should create a new Session instance", initialSession, newSession);
assertNotSame(
"ConnectionManager should create a new Cluster instance", initialCluster, newCluster);
}
}
Loading