kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 80afbe6294e6019e1ee4aa65cdbfd3b8c8871c6d082faf21f6b376677b818f27 |
|---|---|
| Author | Sylvain Lebresne <sylvain@datastax.com> · 2014-02-25 13:21 +01:00 |
| Committer | Sylvain Lebresne <sylvain@datastax.com> · 2014-02-25 13:21 +01:00 |
| Parents | c3118eb39c79 |
4 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -12,6 +12,7 @@ | ||
| 12 | 12 | - [bug] Fix potential NPE in HostConnectionPool (JAVA-231) |
| 13 | 13 | - [bug] Make sure connections are returned to the right pools (JAVA-255) |
| 14 | 14 | - [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) | |
| 15 | 16 | |
| 16 | 17 | |
| 17 | 18 | 1.0.5: |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +9 −2
| @@ -1111,10 +1111,17 @@ | ||
| 1111 | 1111 | } |
| 1112 | 1112 | } |
| 1113 | 1113 | |
| 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) { | |
| 1116 | 1117 | logger.warn("Re-preparing already prepared query {}. Please note that preparing the same query more than once is " |
| 1117 | 1118 | + "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; | |
| 1118 | 1125 | } |
| 1119 | 1126 | |
| 1120 | 1127 | 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 @@ | ||
| 152 | 152 | * session.execute(prepared.bind("someValue")); |
| 153 | 153 | * </pre> |
| 154 | 154 | * 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). | |
| 155 | 161 | * |
| 156 | 162 | * @param statement the statement to prepare |
| 157 | 163 | * @return the prepared statement corresponding to {@code statement}. |
| @@ -179,6 +185,12 @@ | ||
| 179 | 185 | * This method is essentially a shortcut for {@code prepareAsync(statement.getQueryString())}, |
| 180 | 186 | * but with the additional effect that the resulting {@code |
| 181 | 187 | * 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). | |
| 182 | 194 | * |
| 183 | 195 | * @param statement the statement to prepare |
| 184 | 196 | * @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 @@ | ||
| 169 | 169 | case PREPARED: |
| 170 | 170 | ResultMessage.Prepared pmsg = (ResultMessage.Prepared)rm; |
| 171 | 171 | PreparedStatement stmt = PreparedStatement.fromMessage(pmsg, cluster.getMetadata(), query, poolsState.keyspace); |
| 172 | - cluster.manager.addPrepared(stmt); | |
| 172 | + stmt = cluster.manager.addPrepared(stmt); | |
| 173 | 173 | try { |
| 174 | 174 | // All Sessions are connected to the same nodes so it's enough to prepare only the nodes of this session. |
| 175 | 175 | // If that changes, we'll have to make sure this propagate to other sessions too. |