0

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.

Lyuben Todorov
  • 13,987
  • 5
  • 50
  • 69
user2681607
  • 392
  • 1
  • 4
  • 10

1 Answers1

0

I have made a change in the code for getting the session

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());
    }


}

public void close() {
    cluster.shutdown();
}


public static Session getDataStaxSession() {
    synchronized (DataStaxPlugin.class) {
        try {
            if(instance == null)
            {
                instance = new DataStaxPlugin();
                instance.connect();
            } 
            session = cluster.connect();        
            return session;
        } finally {
        }
    }
}


public static void closeDataStaxSession() {
    synchronized (DataStaxPlugin.class) {
        try {
            if(session != null)
            {
                session.shutdown();
                session = null;
            }
        } finally {
        }
    }
}

now every time I am creating a session=cluster.session for every request and shutting down the session after all the processing is done.

I think previously when i was getting the session once and trying to use it again, after some time there was timeout when ever the session was left idle for sometime leading to the exception Is this the right way to implement session for Datastax java driver? Thanks for any comments and feedback

user2681607
  • 392
  • 1
  • 4
  • 10