Elffuss BraidHub

kikocis/java-driver public

Don't lend a connection to a client if setKeyspace failed (JAVA-651).

Fix as part of JAVA-651 to avoid a case where the connection was
released twice. This is also more consistent because the client request
would have likely failed anyway (JAVA-628).
Changeset2f29ae9aea6b4c9f9f57471df6d0d72f4e887521ffa0902a33112ec8422c4dfc
Authorolim7t <omichallat+github@gmail.com> · 2015-02-06 23:52 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-02-11 12:29 +01:00
Parents0707b5d7e98b

Browse the files at this changeset

1 file changed

modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +9 −1
@@ -314,11 +314,12 @@
314314 if (this.keyspace != null && this.keyspace.equals(keyspace))
315315 return;
316316
317+ Future future = null;
317318 try {
318319 logger.trace("{} Setting keyspace {}", this, keyspace);
319320 long timeout = factory.getConnectTimeoutMillis();
320321 // Note: we quote the keyspace below, because the name is the one coming from Cassandra, so it's in the right case already
321- Future future = write(new Requests.Query("USE \"" + keyspace + '"'));
322+ future = write(new Requests.Query("USE \"" + keyspace + '"'));
322323 Message.Response response = Uninterruptibles.getUninterruptibly(future, timeout, TimeUnit.MILLISECONDS);
323324 switch (response.type) {
324325 case RESULT:
@@ -336,9 +337,16 @@
336337 } catch (ConnectionException e) {
337338 throw defunct(e);
338339 } catch (TimeoutException e) {
340+ // We've given up waiting on the future, but it's still running. Cancel to make sure that the request timeout logic
341+ // (readTimeout) will not kick in, because that would release the connection. This will work since connectTimeout is
342+ // generally lower than readTimeout (and if not, we'll get an ExecutionException and defunct below).
343+ future.cancel(true);
339344 logger.warn(String.format("Timeout while setting keyspace on connection to %s. This should not happen but is not critical (it will retried)", address));
345+ // Rethrow so that the caller will not try to use the connection, but do not defunct as we don't want to mark down
346+ throw new ConnectionException(address, "Timeout while setting keyspace on connection");
340347 } catch (BusyConnectionException e) {
341348 logger.warn(String.format("Tried to set the keyspace on busy connection to %s. This should not happen but is not critical (it will retried)", address));
349+ throw new ConnectionException(address, "Tried to set the keyspace on busy connection");
342350 } catch (ExecutionException e) {
343351 throw defunct(new ConnectionException(address, "Error while setting keyspace", e));
344352 }