Skip to content

Commit 3b05815

Browse files
aldobongiomp911de
authored andcommitted
DATACASS-814 - Use CQL in MapPreparedStatementCache.CacheKey instead representation of Object.toString().
We now correctly use the CQL text to cache prepared statements. Previously, we used toString() which was a leftover from the driver migration that now defaults to Object.toString() and so the cache key was not stable. Original pull request: #180.
1 parent fe2b56f commit 3b05815

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/cql/support/MapPreparedStatementCache.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* {@code cql} text. Statement options (idempotency, timeouts) apply from the statement that was initially prepared.
3535
*
3636
* @author Mark Paluch
37+
* @author Aldo Bongio
3738
* @since 2.0
3839
*/
3940
public class MapPreparedStatementCache implements PreparedStatementCache {
@@ -84,7 +85,7 @@ protected Map<CacheKey, PreparedStatement> getCache() {
8485
public PreparedStatement getPreparedStatement(CqlSession session, SimpleStatement statement,
8586
Supplier<PreparedStatement> preparer) {
8687

87-
CacheKey cacheKey = new CacheKey(session, statement.toString());
88+
CacheKey cacheKey = new CacheKey(session, statement.getQuery());
8889

8990
return getCache().computeIfAbsent(cacheKey, key -> preparer.get());
9091
}

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/cql/support/CachedPreparedStatementCreatorUnitTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* Unit tests for {@link CachedPreparedStatementCreator}.
4040
*
4141
* @author Mark Paluch
42+
* @author Aldo Bongio
4243
*/
4344
@ExtendWith(MockitoExtension.class)
4445
@MockitoSettings(strictness = Strictness.LENIENT)
@@ -182,4 +183,17 @@ void shouldCacheAdoptDifferencesInCachedPreparedStatements() {
182183
verify(session).prepare(firstStatement);
183184
verify(session).prepare(secondStatement);
184185
}
186+
187+
@Test // DATACASS-814
188+
void shouldUseCqlTextInCacheKey() {
189+
190+
String cql = "SELECT foo FROM users;";
191+
192+
MapPreparedStatementCache cache = MapPreparedStatementCache.create();
193+
CachedPreparedStatementCreator creator = CachedPreparedStatementCreator.of(cache, cql);
194+
creator.createPreparedStatement(session);
195+
196+
MapPreparedStatementCache.CacheKey cacheKey = cache.getCache().keySet().iterator().next();
197+
assertThat(cacheKey.cql).isSameAs(cql);
198+
}
185199
}

0 commit comments

Comments
 (0)