如何为多线程使用cassandra会话?

时间:2021-03-09 20:56:07

I am using cassandra as backend in web application(in java), where for each user need to create unique session or use single one as in below url?

我在web应用程序中使用cassandra作为后端(在java中),每个用户需要创建唯一会话或使用单个会话,如下面的url?

I read session information in this link. https://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Session.html

我在这个链接中阅读了会话信息。 https://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Session.html

Its saying Session instances are thread-safe. Means one thread at a time can use this instance.

它说Session实例是线程安全的。表示一次一个线程可以使用此实例。

Also written that each session maintains multiple connections to the cluster nodes.

还写了每个会话维护到群集节点的多个连接。

Question is

  1. if only one thread can use on session instance then what is use of creating multiple connection per session to the cluster nodes?
  2. 如果只有一个线程可以在会话实例上使用,那么每个会话创建多个连接到群集节点的用途是什么?

  3. Doesn't it slow downs multithread operations on session?
  4. 它不会减慢会话中的多线程操作吗?

1 个解决方案

#1


2  

You got it wrong, it is safe to share an instance with different threads:

你弄错了,用不同的线程共享一个实例是安全的:

A session holds connections to a Cassandra cluster, allowing it to be queried. Each session maintains multiple connections to the cluster nodes...

会话保持与Cassandra集群的连接,允许查询它。每个会话都维护与群集节点的多个连接......

Session instances are thread-safe and usually a single instance is enough per application.

会话实例是线程安全的,通常每个应用程序的单个实例就足够了。

Thread-safe means that it is safe to share the instance with other threads. It does not mean that you can only use it from a single one.

线程安全意味着与其他线程共享实例是安全的。这并不意味着您只能从一个单独使用它。

#1


2  

You got it wrong, it is safe to share an instance with different threads:

你弄错了,用不同的线程共享一个实例是安全的:

A session holds connections to a Cassandra cluster, allowing it to be queried. Each session maintains multiple connections to the cluster nodes...

会话保持与Cassandra集群的连接,允许查询它。每个会话都维护与群集节点的多个连接......

Session instances are thread-safe and usually a single instance is enough per application.

会话实例是线程安全的,通常每个应用程序的单个实例就足够了。

Thread-safe means that it is safe to share the instance with other threads. It does not mean that you can only use it from a single one.

线程安全意味着与其他线程共享实例是安全的。这并不意味着您只能从一个单独使用它。