我应该调用session.close()和集群。每个Web API调用之后的close()

编程入门 行业动态 更新时间:2024-10-13 04:24:23
本文介绍了我应该调用session.close()和集群。每个Web API调用之后的close()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个Web服务API,允许客户端插入Cassandra。 我阅读了datastax页面上的文档( www.datastax/drivers/java/2.0/com/datastax/driver/core/Session.html )指出,我们应将会话和群集对象保留到结束应用。我想知道在每个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 (www.datastax/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()

本文发布于:2023-10-09 05:41:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474858.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:集群   close   session   Web   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!