Should I call session.close() and cluster. close()

2019-03-27 08:10发布

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条回答
2楼-- · 2019-03-27 09:04

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.

查看更多
登录 后发表回答