When using the Cassandra driver within a Java project, what is the best practice for managing connections? Specifically with respect to whether it's better practice to allow multiple threads to share a single Cluster
instance or to allocate a separate Cluster
instance for each thread that needs to talk to Cassandra.
在Java项目中使用Cassandra驱动程序时,管理连接的最佳做法是什么?特别是关于是否允许多个线程共享单个Cluster实例或为需要与Cassandra通信的每个线程分配单独的Cluster实例更好的做法。
I followed the example code and am setting up my Cluster
instance like:
我按照示例代码设置了我的Cluster实例,如:
Cluster.builder().addContactPoint(HOST).withPort(PORT)
.withCredentials(USER, PASS).build();
So what I'm asking is would the preferred approach be to do something like this (single shared Cluster
instance):
所以我要问的是首选方法是做这样的事情(单个共享集群实例):
private static Cluster _cluster = null;
public static Cluster connect() {
if (_cluster != null && ! _cluster.isClosed()) {
//return the cached instance
return _cluster;
}
//create a new instance
_cluster = Cluster.builder().addContactPoint(HOST).withPort(PORT)
.withCredentials(USER, PASS).build();
return _cluster;
}
...or is it best practice to return multiple Cluster
instances? Like so:
...或者是返回多个Cluster实例的最佳做法?像这样:
public static Cluster connect() {
//every caller gets their own Cluster instance
return Cluster.builder().addContactPoint(HOST).withPort(PORT)
.withCredentials(USER, PASS).build();
}
I guess the points at the core of this question are:
我想这个问题的核心点是:
- Is building a new
Cluster
instance an expensive operation? - 构建新的Cluster实例是一项昂贵的操作吗?
- Will the
Cluster
object internally manage/pool connections to the backing datastore, or does it function more like an abstraction of a single connection? - Cluster对象是内部管理/池连接到后备数据存储区,还是更像是单个连接的抽象?
- Is the
Cluster
object thread-safe? - Cluster对象是线程安全的吗?
1 个解决方案
#1
3
Is building a new Cluster instance an expensive operation?
构建新的Cluster实例是一项昂贵的操作吗?
Calling build to construct a Cluster instance does no networking IO, so it is a non-expensive operation.
调用构建来构建集群实例不会产生网络IO,因此它是一种非昂贵的操作。
Will the Cluster object internally manage/pool connections to the backing datastore, or does it function more like an abstraction of a single connection?
Cluster对象是内部管理/池连接到后备数据存储区,还是更像是单个连接的抽象?
What is expensive is calling cluster.init()
which creates a single connection (control connection) to one of your contact points. cluster.connect()
is even more expensive since it inits the Cluster (if it hasn't been already) and creates a Session
which manages a connection pool (with pool size based on your PoolingOptions) to each discovered host. So yes, a Cluster
has a 'control connection' to manage the state of hosts and each Session
created via Cluster.connect()
will have a connection pool to each host.
值得注意的是调用cluster.init(),它会为您的某个联系点创建一个连接(控制连接)。 cluster.connect()因为它进入集群(如果它还没有)而更加昂贵,并创建一个Session来管理每个发现的主机的连接池(基于您的PoolingOptions的池大小)。所以,是的,集群有一个“控制连接”来管理主机的状态,通过Cluster.connect()创建的每个Session都有一个连接池到每个主机。
Is the Cluster object thread-safe?
Cluster对象是线程安全的吗?
Put simply, yes :)
简单地说,是的:)
4 simple rules when using the DataStax drivers for Cassandra provides further guidance on this topic.
使用Cassandra的DataStax驱动程序时的4个简单规则提供了有关此主题的进一步指导。
#1
3
Is building a new Cluster instance an expensive operation?
构建新的Cluster实例是一项昂贵的操作吗?
Calling build to construct a Cluster instance does no networking IO, so it is a non-expensive operation.
调用构建来构建集群实例不会产生网络IO,因此它是一种非昂贵的操作。
Will the Cluster object internally manage/pool connections to the backing datastore, or does it function more like an abstraction of a single connection?
Cluster对象是内部管理/池连接到后备数据存储区,还是更像是单个连接的抽象?
What is expensive is calling cluster.init()
which creates a single connection (control connection) to one of your contact points. cluster.connect()
is even more expensive since it inits the Cluster (if it hasn't been already) and creates a Session
which manages a connection pool (with pool size based on your PoolingOptions) to each discovered host. So yes, a Cluster
has a 'control connection' to manage the state of hosts and each Session
created via Cluster.connect()
will have a connection pool to each host.
值得注意的是调用cluster.init(),它会为您的某个联系点创建一个连接(控制连接)。 cluster.connect()因为它进入集群(如果它还没有)而更加昂贵,并创建一个Session来管理每个发现的主机的连接池(基于您的PoolingOptions的池大小)。所以,是的,集群有一个“控制连接”来管理主机的状态,通过Cluster.connect()创建的每个Session都有一个连接池到每个主机。
Is the Cluster object thread-safe?
Cluster对象是线程安全的吗?
Put simply, yes :)
简单地说,是的:)
4 simple rules when using the DataStax drivers for Cassandra provides further guidance on this topic.
使用Cassandra的DataStax驱动程序时的4个简单规则提供了有关此主题的进一步指导。