简体   繁体   English

Java中与Cassandra的多个并发连接

[英]Multiple concurrent connections to Cassandra in Java

I'm developing a multi-threaded application in which each thread accesses Cassandra in Java. 我正在开发一个多线程应用程序,其中每个线程都使用Java访问Cassandra。 Shall I create multiple clusters and close them after each access or multiple sessions? 我应该创建多个群集并在每次访问或多个会话后将其关闭吗? Can I create the cluster or the session as static data members? 我可以将集群或会话创建为静态数据成员吗?

Here is the log I got when declaring both as static: 这是将两者都声明为静态时得到的日志:

14:40:50.361 [cluster1-nio-worker-0] DEBUG com.datastax.driver.core.Connection - Connection[localhost/127.0.0.1:9042-1, inFlight=0, closed=false] was inactive for 30 seconds, sending heartbeat
14:40:50.361 [cluster1-nio-worker-0] DEBUG com.datastax.driver.core.Connection - Connection[localhost/127.0.0.1:9042-1, inFlight=0, closed=false] heartbeat query succeeded

If you are writing a multi-thread application I would create Cluster and Session as static member . 如果您正在编写多线程应用程序,则可以将Cluster和Session创建为static成员 Session maintain multiple threads per instance and is thread-safe . 会话每个实例维护多个线程,并且是线程安全的 I would use one Session object per keyspace . 我将每个键空间使用一个Session对象。

From cassandra-java-driver docs: 从cassandra-java-driver文档:

Session instances are thread-safe and usually a single instance is enough per application. 会话实例是线程安全的,通常每个应用程序一个实例就足够了。 As a given session can only be "logged" into one keyspace at a time (where the "logged" keyspace is the one used by query if the query doesn't explicitely use a fully qualified table name), it can make sense to create one session per keyspace used. 由于一次给定的会话一次只能“登录”到一个键空间中(如果查询未明确使用完全限定的表名,则“记录的”键空间是查询所使用的键空间),因此创建每个键空间使用一个会话。 This is however not necessary to query multiple keyspaces since it is always possible to use a single session with fully qualified table name in queries. 但是,查询多个键空间不是必需的,因为在查询中始终可以使用具有完全限定的表名的单个会话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM