【问题标题】:Spring Cassandra is CachedPreparedStatementCreator class threadsafe?Spring Cassandra 是 CachedPreparedStatementCreator 类线程安全的吗?
【发布时间】:2016-05-01 00:19:39
【问题描述】:

我们正在使用 Spring Cassandra 和 datastax 驱动程序使用 CachedPreparedStatementCreator/PreparedStatement 向 Cassandra 发出请求。在启动时,这些 WARN 消息会显示在日志文件中(还有其他的,这只是一个示例)

:WARN lg:com.datastax.driver.core.Cluster - 重新准备已准备好的查询插入到活动(id)值(?)使用 TTL?。请注意,多次准备同一个查询通常是一种反模式,并且可能会影响性能。考虑只准备一次声明。

准备好的语句是使用 CachedPreparedStatementCreator 类创建的。查看源代码,似乎存在“竞争条件”并且实现不是线程安全的。

         Map<String, PreparedStatement> sessionMap = psMap.get(session);

        if (sessionMap == null) {

              sessionMap = new ConcurrentHashMap<String, PreparedStatement>();

              psMap.put(session, sessionMap);

        }



        PreparedStatement pstmt = sessionMap.get(keyspaceCQLKey.toString());

        if (pstmt == null) {

              log.debug("No Cached PreparedStatement found...Creating and Caching");

              pstmt = session.prepare(this.cql);

              sessionMap.put(keyspaceCQLKey.toString(), pstmt);

        } else {

              log.debug("Found cached PreparedStatement");

        }

这门课的目的是什么?它是线程安全的吗?有人会知道有关此缓存性能的任何信息吗?

【问题讨论】:

  • Hopper SR2 版本修复了这个问题。

标签: java spring cassandra datastax


【解决方案1】:

在检查语句是否存在和创建新的预处理语句之间肯定存在竞争条件。我会寻求类似于ReentrantReadWriteLock 文档中的解决方案。

请注意,在实现写锁定后,会验证该值仍然不存在。这将解决您在此处看到的情况。

考虑在Spring Data CassandraJIRA 提交工单或在Github 提交 PR。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 2012-12-18
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多