I am using cassandra1.2 with datastax java client 1.0.3 Using the samples for the java client from datastax I have created a class which will return me the session
public class DataStaxPlugin {
public static final Logger LOGGER = Logger.getLogger(DataStaxPlugin.class.getName());
private static Cluster cluster;
private static Session session = null;
private static String node;
private static String port;
private static DataStaxPlugin instance = null;
protected DataStaxPlugin() {
// Exists only to defeat instantiation.
}
public void connect() {
node = ReadPropertiesFile.getProperty("db.server.name");
port = ReadPropertiesFile.getProperty("db.server.port");
LOGGER.debug("Connecting to DB server: " + node);
LOGGER.debug("port: " + port);
cluster = Cluster.builder().withPort(Integer.parseInt(port))
.addContactPoint(node).build();
Metadata metadata = cluster.getMetadata();
System.out.printf("Connected to cluster: %s\n",
metadata.getClusterName());
for (Host host : metadata.getAllHosts()) {
System.out.printf("Datatacenter: %s; Host: %s; Rack: %s\n",
host.getDatacenter(), host.getAddress(), host.getRack());
}
session = cluster.connect();
}
public void close() {
cluster.shutdown();
}
public static Session getDataStaxSession() {
synchronized (DataStaxPlugin.class) {
try {
if (instance == null) {
instance = new DataStaxPlugin();
instance.connect();
}
return session;
} finally {
}
}
}
}
I use the getDatastaxSession function to get the session and do one insert in the database. The first few inserts works fine but after sometime I get nullpointer exception as show below:
Exception in thread "Cassandra Java Driver worker-5" java.lang.NullPointerException
at com.datastax.driver.core.Connection$Future.onException(Connection.java:602)
at com.datastax.driver.core.RequestHandler.setFinalException(RequestHandler.java:219)
at com.datastax.driver.core.RequestHandler.sendRequest(RequestHandler.java:103)
at com.datastax.driver.core.RequestHandler$1.run(RequestHandler.java:170)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
Is this exception due to Session being null/timedout? Please let me know if I am creating or managing the session in a wrong way. Or please let me know if there is any problem with my code.