Elffuss BraidHub

kikocis/java-driver public

Always return the PrepararedStatement object that is cached internally

JAVA-263 #fixes
Changeset80afbe6294e6019e1ee4aa65cdbfd3b8c8871c6d082faf21f6b376677b818f27
AuthorSylvain Lebresne <sylvain@datastax.com> · 2014-02-25 13:21 +01:00
CommitterSylvain Lebresne <sylvain@datastax.com> · 2014-02-25 13:21 +01:00
Parentsc3118eb39c79

Browse the files at this changeset

4 files changed

modified driver-core/CHANGELOG.rst +1 −0
@@ -12,6 +12,7 @@
1212 - [bug] Fix potential NPE in HostConnectionPool (JAVA-231)
1313 - [bug] Make sure connections are returned to the right pools (JAVA-255)
1414 - [bug] Use date string in query build to work-around CASSANDRA-6718 (JAVA-264)
15+- [bug] Always return the PreparedStatement object that is cache internally (JAVA-263)
1516
1617
1718 1.0.5:
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +9 −2
@@ -1111,10 +1111,17 @@
11111111 }
11121112 }
11131113
1114- public void addPrepared(PreparedStatement stmt) {
1115- if (preparedQueries.putIfAbsent(stmt.id, stmt) != null)
1114+ public PreparedStatement addPrepared(PreparedStatement stmt) {
1115+ PreparedStatement previous = preparedQueries.putIfAbsent(stmt.id, stmt);
1116+ if (previous != null) {
11161117 logger.warn("Re-preparing already prepared query {}. Please note that preparing the same query more than once is "
11171118 + "generally an anti-pattern and will likely affect performance. Consider preparing the statement only once.", stmt.getQueryString());
1119+
1120+ // The one object in the cache will get GCed once it's not referenced by the client anymore since we use a weak reference.
1121+ // So we need to make sure that the instance we do return to the user is the one that is in the cache.
1122+ return previous;
1123+ }
1124+ return stmt;
11181125 }
11191126
11201127 private void prepareAllQueries(Host host) throws InterruptedException {
modified driver-core/src/main/java/com/datastax/driver/core/Session.java +12 −0
@@ -152,6 +152,12 @@
152152 * session.execute(prepared.bind("someValue"));
153153 * </pre>
154154 * the final execution will be performed with Quorum consistency.
155+ * <p>
156+ * Please note however that if the same CQL statement is prepared more than
157+ * once, all calls to this method will return the same {@code PreparedStatement}
158+ * object (which imply that this unique {@code PreparedStatement} object will
159+ * inherit the query properties of the last statement for which this method
160+ * has been called).
155161 *
156162 * @param statement the statement to prepare
157163 * @return the prepared statement corresponding to {@code statement}.
@@ -179,6 +185,12 @@
179185 * This method is essentially a shortcut for {@code prepareAsync(statement.getQueryString())},
180186 * but with the additional effect that the resulting {@code
181187 * PreparedStamenent} will inherit the query properties set on {@code statement}.
188+ * <p>
189+ * Please note however that if the same CQL statement is prepared more than
190+ * once, all calls to this method will return the same {@code PreparedStatement}
191+ * object (which imply that this unique {@code PreparedStatement} object will
192+ * inherit the query properties of the last statement for which this method
193+ * has been called).
182194 *
183195 * @param statement the statement to prepare
184196 * @return a future on the prepared statement corresponding to {@code statement}.
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +1 −1
@@ -169,7 +169,7 @@
169169 case PREPARED:
170170 ResultMessage.Prepared pmsg = (ResultMessage.Prepared)rm;
171171 PreparedStatement stmt = PreparedStatement.fromMessage(pmsg, cluster.getMetadata(), query, poolsState.keyspace);
172- cluster.manager.addPrepared(stmt);
172+ stmt = cluster.manager.addPrepared(stmt);
173173 try {
174174 // All Sessions are connected to the same nodes so it's enough to prepare only the nodes of this session.
175175 // If that changes, we'll have to make sure this propagate to other sessions too.