0

I am using DataStax API to read records from Cassandra database,Some times execute method in Session is showing strange behavior. Sometimes it results empty result set, sometimes it returns correct result set.

Here is my code

 //Create session instance, using Singleton pattern public synchronized static Static getSession(){ if(session !=null){ //Not sending all the clusters Cluster cluster = Cluster.builder().withPort(myPort).addClusterPoints(clusters).withCredentials("username","password"). withSocketOptions(new SocketOptions().setReadTimeoutMillis(30000).setConnectTimeoutMillis(30000)).build(); session = cluster.connect("database"); } else{ return session; } } //get the session and execute query and return resultset public void executeQuery(){ Session session = getSession(); BoundStatement boundStatement = .. ResultSet result = session.execute(boundStatement); System.out.println(result.isExhausted); // true System.out.println(result.isFullyFetched); // true System.out.println(result.all().size()); /// **0 sometimes, correct count sometimes** } 

I am not sending all clusters in addClusterPoints due to some reasons. Does it create any problem? But i am getting data some times.

1 Answer 1

1

This may depend on your replication factor (RF) & the state of your cluster. If you have RF > 1, then default TokenAware/DCAware policy will try to fetch data from any of nodes that contain replica. By default, query is executed with consistency level LOCAL_ONE - this means that answer from one replica is enough, and in that case it could be possible that replica that is contacted, doesn't have data that you need.

This happens if one of the replicas was offline for time longer than the size of time window for hints (3 hours by default), and that repair wasn't performed afterwards. To mitigate the problem, run the nodetool repair (precise recommendation depends on your version of DSE - if you're using it).

Sign up to request clarification or add additional context in comments.

2 Comments

Do I need to change my java code? From java code side is that fine? because when I run the same query in database i get the results
You can change your code to use consistency level of LOCAL_QUORUM, for example, but this will be slower. When you're running query from cqlsh, it may connect to node that has data. I recommend to perform repair anyway

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.