Elffuss BraidHub

kikocis/java-driver public

Fix NPE when a connection fails during pool construction (JAVA-503).
Changeset47198e2e04ca8b5355c0058b05336dde2c8dbcbb59f835288787b7d1df3171f4
Authorolim7t <omichallat+github@gmail.com> · 2014-10-15 18:24 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-10-15 19:28 +02:00
Parents736d1b430942

Browse the files at this changeset

2 files changed

modified driver-core/CHANGELOG.rst +1 −0
@@ -14,6 +14,7 @@
1414 - [improvement] Expose close method on load balancing policy (JAVA-463)
1515 - [improvement] Allow load balancing policy to trigger refresh for a single host (JAVA-459)
1616 - [bug] Expose an API to cancel reconnection attempts (JAVA-493)
17+- [bug] Fix NPE when a connection fails during pool construction (JAVA-503)
1718
1819
1920 2.0.6:
modified driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java +10 −0
@@ -29,6 +29,7 @@
2929 import java.util.concurrent.locks.Lock;
3030 import java.util.concurrent.locks.ReentrantLock;
3131
32+import com.google.common.collect.Lists;
3233 import com.google.common.util.concurrent.MoreExecutors;
3334 import org.slf4j.Logger;
3435 import org.slf4j.LoggerFactory;
@@ -87,6 +88,12 @@
8788 Thread.currentThread().interrupt();
8889 // If asked to interrupt, we can skip opening core connections, the pool will still work.
8990 // But we ignore otherwise cause I'm not sure we can do much better currently.
91+ } catch (RuntimeException e) {
92+ // Rethrow but make sure to properly close the connections in our temporary list.
93+ for (PooledConnection connection : l) {
94+ connection.closeAsync().force();
95+ }
96+ throw e;
9097 }
9198 this.connections = new CopyOnWriteArrayList<PooledConnection>(l);
9299 this.open = new AtomicInteger(connections.size());
@@ -410,6 +417,9 @@
410417 }
411418
412419 private List<CloseFuture> discardAvailableConnections() {
420+ // This can happen if creating the connections in the constructor fails
421+ if (connections == null)
422+ return Lists.newArrayList(CloseFuture.immediateFuture());
413423
414424 List<CloseFuture> futures = new ArrayList<CloseFuture>(connections.size());
415425 for (final PooledConnection connection : connections) {