This doc guides how to use Cassandra prepared and bound statements.线程安全
它说:
你应该准备只有一次,并在你的应用程序 缓存的PreparedStatement(它是线程安全的)。 ... BoundStatement不是 线程安全的。你可以用不同的 参数多次重复使用的实例,但只能从单个线程且仅当您使用 同步调用:
BoundStatement bound = ps1.bind();
// This is safe:
bound.setString("sku", "324378");
session.execute(bound);
bound.setString("sku", "324379");
session.execute(bound);
// This is NOT SAFE. executeAsync runs concurrently with your code, so the first execution might actually read the
// values after the second setString call, and you would insert 324381 twice:
bound.setString("sku", "324380");
session.executeAsync(bound);
bound.setString("sku", "324381");
session.executeAsync(bound);
很显然,上面是不是线程安全的,但如果我们改变这样的代码:
BoundStatement bound1 = ps1.bind();
BoundStatement bound2 = ps1.bind();
bound1.setString("sku", "324380");
session.executeAsync(bound1);
bound2.setString("sku", "324381");
session.executeAsync(bound2);
那就是:几个线程,每个线程使用PreparedStatement的共同使用自己BoundStatement。
1)此线程安全吗?
2)这是否推荐使用预处理语句执行并行执行的方法?或者是BoundStatements昂贵/慢创建/消耗大量内存等原因,以保持其数量低?
感谢您的明确答案。希望他们将这些信息添加到下一版Cassandra手册中。 – user4955663