Skip to content

Commit bc84386

Browse files
GregBestlandolim7t
authored andcommitted
JAVA-1948: Close session properly when LBP fails to initialize
1 parent b618409 commit bc84386

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### 4.0.0-beta2 (in progress)
66

7+
- [bug] JAVA-1948: Close session properly when LBP fails to initialize
78
- [improvement] JAVA-1949: Improve error message when contact points are wrong
89
- [improvement] JAVA-1956: Add statementsCount accessor to BatchStatementBuilder
910
- [bug] JAVA-1946: Ignore protocol version in equals comparison for UdtValue/TupleValue

core/src/main/java/com/datastax/oss/driver/internal/core/session/DefaultSession.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,11 @@ private void afterInitialSchemaRefresh(CqlIdentifier keyspace) {
366366
}
367367
});
368368
} catch (Throwable throwable) {
369-
initFuture.completeExceptionally(throwable);
369+
forceCloseAsync()
370+
.whenComplete(
371+
(v, error) -> {
372+
initFuture.completeExceptionally(throwable);
373+
});
370374
}
371375
}
372376

integration-tests/src/test/java/com/datastax/oss/driver/api/core/ConnectIT.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package com.datastax.oss.driver.api.core;
1717

18+
import static com.datastax.oss.driver.api.testinfra.utils.ConditionChecker.checkThat;
19+
import static java.util.concurrent.TimeUnit.SECONDS;
20+
import static junit.framework.TestCase.fail;
1821
import static org.assertj.core.api.Assertions.assertThat;
1922

2023
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
@@ -98,6 +101,30 @@ public void should_wait_for_contact_points_if_reconnection_enabled() throws Exce
98101
session.close();
99102
}
100103

104+
/**
105+
* Test for JAVA-1948. This ensures that when the LBP initialization fails that any connections
106+
* are cleaned up appropriately.
107+
*/
108+
@Test
109+
public void should_cleanup_on_lbp_init_failure() {
110+
try {
111+
DriverConfigLoader loader =
112+
SessionUtils.configLoaderBuilder()
113+
.without(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER)
114+
.build();
115+
CqlSession.builder()
116+
.addContactPoints(simulacronRule.getContactPoints())
117+
.withConfigLoader(loader)
118+
.build();
119+
fail("Should have thrown a DriverException for no DC with explicit contact point");
120+
} catch (DriverException ignored) {
121+
}
122+
// One second should be plenty of time for connections to close server side
123+
checkThat(() -> simulacronRule.cluster().getConnections().getConnections().isEmpty())
124+
.before(1, SECONDS)
125+
.becomesTrue();
126+
}
127+
101128
@SuppressWarnings("unchecked")
102129
private CompletionStage<? extends Session> newSessionAsync(
103130
SimulacronRule serverRule, DriverConfigLoader loader) {

0 commit comments

Comments
 (0)