Should I call session.close() and cluster. close() after each web API call

狂风中的少年 提交于 2019-12-04 19:09:13

问题


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?


回答1:


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.

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.

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.



来源:https://stackoverflow.com/questions/28211409/should-i-call-session-close-and-cluster-close-after-each-web-api-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!