4
votes

I am using Datastax Java Driver. There is a tutorial to use the same.

What I do not understand is how would one close the connection to cassandra? There is no close method available and I assume we do not want to shutdown the Session as it it expected to be one per application.

Regards Gaurav

2

2 Answers

4
votes

tl;dr Calling shutdown on Session is the correct way to close connections.

You should be safe to keep a Session object at hand and shut it when you're finished with Cassandra - this can be long-lived. You can get individual connections in the form of Session objects as you need them and shut them down when done but ideally you should only create one Session object per application. A Session is a fairly heavyweight object that keeps pools of pools of connections to the node in the cluster, so creating multiple of those will be inefficient (and unnecessary) (taken verbatim from advice given by Sylvain Lebresne on the mailing list). If you forget to shut down the session(s) they will be all closed when you call shutdown on your Cluster instance... really simple example below:

Cluster cluster = Cluster.builder().addContactPoints(host).withPort(port).build();
Session session = cluster.connect(keyspace);

// Do something with session... 

session.shutdown();
cluster.shutdown();
1
votes

See here - http://www.datastax.com/drivers....

The driver uses connections in an asynchronous manner. Meaning that multiple requests can be submitted on the same connection at the same time. This means that the driver only needs to maintain a relatively small number of connections to each Cassandra host. These options allow the driver to control how many connections are kept exactly.

For each host, the driver keeps a core pool of connections open at all times determined by calling . If the use of those connections reaches a configurable threshold , more connections are created up to the configurable maximum number of connections. When the pool exceeds the maximum number of connections, connections in excess are reclaimed if the use of opened connections drops below the configured threshold

Each of these parameters can be separately set for LOCAL and REMOTE hosts (HostDistance). For IGNORED hosts, the default for all those settings is 0 and cannot be changed.