问题描述
我有一个Web服务API,允许客户端插入Cassandra。
我阅读了datastax页面上的文档()指出,我们应将会话和群集对象保留到结束应用。我想知道在每个Web API调用之后应该调用session.close()和cluster.close()还是保持会话直到关闭Web服务器?
I have an webservice API allowing client to insert into Cassandra.I read the document on the page of datastax (http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/Session.html) stating that we should keep the session and cluster object till the end of the application. I was wondering should I call session.close() and cluster.close() after each web API call or I just keep the session until I shutdown web server?
推荐答案
我建议您每次收到请求时都不要创建 Session
。每次通过 Cluster.connect
创建 Session
时,Java驱动程序都会创建一个到多个主机的连接池
I would advise against creating a Session
each time you receive a request. Each time you create a Session
via Cluster.connect
the java-driver will create a connection pool to a number of Hosts to your Cassandra Cluster.
例如,使用默认设置,如果单个数据中心中有8个cassandra节点,将使用驱动程序2.0.9版本创建每个主机8个池化连接(在下一版本中将更改为2个)。每次创建 Session
都会创建64个连接。
For example, using default settings, if you have 8 cassandra nodes in a single datacenter, with the 2.0.9 version of the driver it will create 8 pooled connections to each Host (this will change to 2 in the next version). This would create 64 connections each time you create a Session
.
最好共享一个会话。该驱动程序可以管理每个连接多个请求(在2.0.x中默认为每个连接默认128个),因此无需担心共享单个 Session
对象时的争用。
It would be more preferable to have a shared Session
that your web server can use. The driver can manage multiple requests per connection (default 128 per connection by default in 2.0.x), so no need to worry about contention in sharing a single Session
object.
这篇关于我应该调用session.close()和集群。每个Web API调用之后的close()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!