12

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?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Minh Vu
  • 211
  • 1
  • 3
  • 10
  • 1
    Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advanced". – John Saunders Jan 29 '15 at 18:26

1 Answers1

16

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.

Andy Tolbert
  • 11,418
  • 1
  • 30
  • 45
  • 4
    Besides what Andy describes, this post http://www.datastax.com/dev/blog/4-simple-rules-when-using-the-datastax-drivers-for-cassandra provides you with a set of basic rules to ensure that you are making the best use of the API – Alex Popescu Jan 30 '15 at 07:49
  • 1
    From you advise, I would create a session and a cluster and keep them alive till the end of my web server life? That means the session will be alive forever? We push the life time of the session and cluster to the server side? Is this a good idea? What if we have so many clients connecting to Cassandra at the same time? – Minh Vu Feb 03 '15 at 04:11
  • 1
    Correct. How many clients are you planning on connecting to cassandra? There is configurability in the driver with regards to how many connections you create to each node. If you are concerned about having a large number of clients connecting to cassandra, it may be a good idea to have intermediate services that interface to cassandra for you. A good example of this is mobile apps, you wouldn't want each mobile app to have a connection to cassandra right? You'd have a backend server that the mobile app communicates with that makes requests to cassandra on behalf of the app. – Andy Tolbert Feb 03 '15 at 20:30
  • Hi @AndyTolbert, I means that we will push the management of Cassandra session life time to the Cassandra driver. When you open a session, cassandra must keep the connection forever right? Is it a good idea? – Minh Vu Mar 13 '15 at 09:35
  • @MinhVu, that will work fine. the Driver manages core and max connections per host via PoolingOptions (http://www.datastax.com/drivers/java/2.0/com/datastax/driver/core/PoolingOptions.html) so it'll only manage as many connections it needs. – Andy Tolbert Mar 13 '15 at 17:17