Elffuss BraidHub

kikocis/java-driver public

Fail fast if statement needs to be reprepared on the wrong keyspace (JAVA-651).

Fixed as part of JAVA-651 to avoid calls to setKeyspace that could
timeout and lead to releasing the connection twice. This also addresses
issues raised in JAVA-633.
Changeset99eff8eb2fee30fa9bedd9ccb34d884d82d5dca7856daeb8081747c4e53d36a0
Authorolim7t <omichallat+github@gmail.com> · 2015-02-06 16:18 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-02-11 12:39 +01:00
Parentsb71ec2203d70

Browse the files at this changeset

1 file changed

modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +13 −21
@@ -403,31 +403,23 @@
403403 return;
404404 }
405405
406- logger.info("Query {} is not prepared on {}, preparing before retrying executing. "
407- + "Seeing this message a few times is fine, but seeing it a lot may be source of performance problems",
408- toPrepare.getQueryString(), connection.address);
409406 String currentKeyspace = connection.keyspace();
410407 String prepareKeyspace = toPrepare.getQueryKeyspace();
411- // This shouldn't happen in normal use, because a user shouldn't try to execute
412- // a prepared statement with the wrong keyspace set. However, if it does, we'd rather
413- // prepare the query correctly and let the query executing return a meaningful error message
414- if (prepareKeyspace != null && (currentKeyspace == null || !currentKeyspace.equals(prepareKeyspace)))
415- {
416- logger.debug("Setting keyspace for prepared query to {}", prepareKeyspace);
417- connection.setKeyspace(prepareKeyspace);
408+ if (prepareKeyspace != null && (currentKeyspace == null || !currentKeyspace.equals(prepareKeyspace))) {
409+ // This shouldn't happen in normal use, because a user shouldn't try to execute
410+ // a prepared statement with the wrong keyspace set.
411+ // Fail fast (we can't change the keyspace to reprepare, because we're using a pooled connection
412+ // that's shared with other requests).
413+ throw new IllegalStateException(String.format("Statement was prepared on keyspace %s, can't execute it on %s (%s)",
414+ toPrepare.getQueryKeyspace(), connection.keyspace(), toPrepare.getQueryString()));
418415 }
419416
420- try {
421- releaseConnection = false; // we're reusing it for the prepare call
422- write(connection, prepareAndRetry(toPrepare.getQueryString()));
423- } finally {
424- // Always reset the previous keyspace if needed
425- if (connection.keyspace() == null || !connection.keyspace().equals(currentKeyspace))
426- {
427- logger.debug("Setting back keyspace post query preparation to {}", currentKeyspace);
428- connection.setKeyspace(currentKeyspace);
429- }
430- }
417+ logger.info("Query {} is not prepared on {}, preparing before retrying executing. "
418+ + "Seeing this message a few times is fine, but seeing it a lot may be source of performance problems",
419+ toPrepare.getQueryString(), connection.address);
420+
421+ releaseConnection = false; // we're reusing it for the prepare call
422+ write(connection, prepareAndRetry(toPrepare.getQueryString()));
431423 // we're done for now, the prepareAndRetry callback will handle the rest
432424 return;
433425 default: