kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 4ef2a8c7363bb84dc59b31e0e8bcfef9efce101004d437e2b4989b9c26a2a57f |
|---|---|
| Author | Kevin Gallardo <kevin.gallardo@datastax.com> · 2015-03-10 11:26 UTC |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-04-01 11:18 +02:00 |
| Parents | 7b0b25b50034 |
3 files changed
modified driver-core/CHANGELOG.rst +2 −0
| @@ -31,6 +31,8 @@ | ||
| 31 | 31 | - [improvement] Slow Query Logger (JAVA-646) |
| 32 | 32 | - [improvement] Exclude some errors from measurements in LatencyAwarePolicy |
| 33 | 33 | (JAVA-698) |
| 34 | +- [bug] Fix issue when executing a PreparedStatement from another cluster | |
| 35 | + (JAVA-641) | |
| 34 | 36 | |
| 35 | 37 | Merged from 2.0.9_fixes branch: |
| 36 | 38 |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +5 −0
| @@ -29,6 +29,7 @@ | ||
| 29 | 29 | import org.slf4j.LoggerFactory; |
| 30 | 30 | |
| 31 | 31 | import com.datastax.driver.core.exceptions.DriverInternalError; |
| 32 | +import com.datastax.driver.core.exceptions.InvalidQueryException; | |
| 32 | 33 | import com.datastax.driver.core.exceptions.UnsupportedFeatureException; |
| 33 | 34 | import com.datastax.driver.core.policies.LoadBalancingPolicy; |
| 34 | 35 | import com.datastax.driver.core.policies.ReconnectionPolicy; |
| @@ -491,6 +492,10 @@ | ||
| 491 | 492 | return new Requests.Query(qString, options); |
| 492 | 493 | } else if (statement instanceof BoundStatement) { |
| 493 | 494 | 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 | + } | |
| 494 | 499 | boolean skipMetadata = protoVersion != 1 && bs.statement.getPreparedId().resultSetMetadata != null; |
| 495 | 500 | Requests.QueryProtocolOptions options = new Requests.QueryProtocolOptions(cl, Arrays.asList(bs.values), skipMetadata, fetchSize, usedPagingState, scl); |
| 496 | 501 | 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 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import java.net.InetAddress; | |
| 18 | 19 | import java.util.*; |
| 19 | 20 | |
| 21 | +import com.google.common.collect.ImmutableList; | |
| 20 | 22 | import org.testng.annotations.Test; |
| 21 | 23 | |
| 22 | 24 | import static org.assertj.core.api.Assertions.assertThat; |
| 23 | 25 | import static org.testng.Assert.assertEquals; |
| 24 | 26 | import static org.testng.Assert.assertTrue; |
| 25 | 27 | |
| 28 | +import com.datastax.driver.core.exceptions.InvalidQueryException; | |
| 26 | 29 | import com.datastax.driver.core.exceptions.NoHostAvailableException; |
| 27 | 30 | import com.datastax.driver.core.exceptions.UnsupportedFeatureException; |
| 28 | 31 | |
| @@ -433,4 +436,16 @@ | ||
| 433 | 436 | BoundStatement bs = ps.bind(1); |
| 434 | 437 | assertThat(bs.getRoutingKey()).isNotNull(); |
| 435 | 438 | } |
| 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 | + } | |
| 436 | 451 | } |