Search code examples
javacassandrasonarqubedatastax-java-driver

Close ResultSet and Statement resources (datastax, no close() method)


Hello World !

I'm in trouble trying to close some datastax resources (Statement, ResultSet). Sonar is yelling at me to close those resources after i use them.

(for information after i use this myMethod() i call a System.exit(0)) Bu anyway, I would like to do it according to Sonar

session.close() is not enough since it appears to let Statement and ResultSet.

/!\ ResultSet and Statement are from com.datastax.com.driver and these close() method doesn't exist on them. (different from java.sql)

I think a session.getCluster.close() would do, but I don't want to close the Cluster.

What would be the right way to close those resources properly ?

import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.Statement;
public void myMethod() {

    Statement statement = session.prepare("select * from .....").bind();
    ResultSet rs = session.execute(statement);

    // doSomethingWithThisResultSet() ...

    session.close();
}

Thanks in advance for your help !


Solution

  • The ResultSet is not "linked" to a connection or any other resource that needs closing. It's backed by an array. See: https://github.com/datastax/java-driver/blob/2.1/driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java

    credits: https://groups.google.com/a/lists.datastax.com/forum/#!topic/java-driver-user/yjDP1xeYyYM

    Statement contains only statement ID, so you don't need to close it too.