Elffuss BraidHub

kikocis/java-driver public

Merge pull request #292 from newkek/JAVA-641

Avoid using prepared statement on different clusters (JAVA-641)
Changesetcbb199d764d92282af05d280c213149a649a8c19d52d06cd4c6b72abb389f3ed
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-04-01 11:20 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-04-01 11:20 +02:00
Parents7b0b25b50034, 4ef2a8c7363b

Browse the files at this changeset

3 files changed

modified driver-core/CHANGELOG.rst +2 −0
@@ -31,6 +31,8 @@
3131 - [improvement] Slow Query Logger (JAVA-646)
3232 - [improvement] Exclude some errors from measurements in LatencyAwarePolicy
3333 (JAVA-698)
34+- [bug] Fix issue when executing a PreparedStatement from another cluster
35+ (JAVA-641)
3436
3537 Merged from 2.0.9_fixes branch:
3638
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +5 −0
@@ -29,6 +29,7 @@
2929 import org.slf4j.LoggerFactory;
3030
3131 import com.datastax.driver.core.exceptions.DriverInternalError;
32+import com.datastax.driver.core.exceptions.InvalidQueryException;
3233 import com.datastax.driver.core.exceptions.UnsupportedFeatureException;
3334 import com.datastax.driver.core.policies.LoadBalancingPolicy;
3435 import com.datastax.driver.core.policies.ReconnectionPolicy;
@@ -491,6 +492,10 @@
491492 return new Requests.Query(qString, options);
492493 } else if (statement instanceof BoundStatement) {
493494 BoundStatement bs = (BoundStatement)statement;
495+ if (!cluster.manager.preparedQueries.containsKey(bs.statement.getPreparedId().id)) {
496+ throw new InvalidQueryException(String.format("Tried to execute unknown prepared query : %s. "
497+ + "You may have used a PreparedStatement that was created with another Cluster instance.", bs.statement.getPreparedId().id));
498+ }
494499 boolean skipMetadata = protoVersion != 1 && bs.statement.getPreparedId().resultSetMetadata != null;
495500 Requests.QueryProtocolOptions options = new Requests.QueryProtocolOptions(cl, Arrays.asList(bs.values), skipMetadata, fetchSize, usedPagingState, scl);
496501 return new Requests.Execute(bs.statement.getPreparedId().id, options);
modified driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java +15 −0
@@ -15,14 +15,17 @@
1515 */
1616 package com.datastax.driver.core;
1717
18+import java.net.InetAddress;
1819 import java.util.*;
1920
21+import com.google.common.collect.ImmutableList;
2022 import org.testng.annotations.Test;
2123
2224 import static org.assertj.core.api.Assertions.assertThat;
2325 import static org.testng.Assert.assertEquals;
2426 import static org.testng.Assert.assertTrue;
2527
28+import com.datastax.driver.core.exceptions.InvalidQueryException;
2629 import com.datastax.driver.core.exceptions.NoHostAvailableException;
2730 import com.datastax.driver.core.exceptions.UnsupportedFeatureException;
2831
@@ -433,4 +436,16 @@
433436 BoundStatement bs = ps.bind(1);
434437 assertThat(bs.getRoutingKey()).isNotNull();
435438 }
439+
440+ @Test(groups="short", expectedExceptions = InvalidQueryException.class)
441+ public void should_fail_when_prepared_on_another_cluster() throws Exception {
442+ Cluster otherCluster = Cluster.builder()
443+ .addContactPointsWithPorts(ImmutableList.of(hostAddress))
444+ .build();
445+ PreparedStatement pst = otherCluster.connect().prepare("select * from system.peers where inet = ?");
446+ BoundStatement bs = pst.bind().setInet(0, InetAddress.getByName("localhost"));
447+
448+ // We expect that the error gets detected without a roundtrip to the server, so use executeAsync
449+ session.executeAsync(bs);
450+ }
436451 }