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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With