2

I have this code

    UUID notUuid = UUIDs.timeBased();        
    PreparedStatement   pstmt           = cqlSession.prepare("INSERT INTO mytable(userId, notifId, notification, time, read, deleted) VALUES(?, ?, ?, ?, ?, ?)");
    BoundStatement      boundStatement  = new BoundStatement(pstmt);
    cqlSession.execute(boundStatement.bind(userId, notUuid, notfMsg, System.currentTimeMillis(), MigificConstants.UNREAD, "false"));

when i run this code, in the log it shows

Re-preparing already prepared query INSERT INTO mytable(userId, notifId, notification, time, read, deleted) VALUES(?, ?, ?, ?, ?, ?). Please note that preparing the same query more than once is generally an anti-pattern and will likely affect performance. Consider preparing the statement only once.
Erick Ramirez
  • 13,964
  • 1
  • 18
  • 23
Manish Kumar
  • 10,214
  • 25
  • 77
  • 147
  • Here is the answer: [Re-using PreparedStatement when using Datastax Cassandra Driver?][1] [1]: http://stackoverflow.com/questions/22915840/re-using-preparedstatement-when-using-datastax-cassandra-driver – jcflorezr Jan 23 '15 at 19:07

1 Answers1

2

Is this code part of a method that is called multiple times?

Statements need only be prepared once per Cluster instance, you should do it in some initialization part of your application.

Olivier Michallat
  • 2,302
  • 11
  • 13
  • yes this is called every time a new message is received. so shall i initialize `cqlSession.prepare` line at the start of application only. i have so many this type of statement – Manish Kumar Nov 17 '14 at 17:14
  • 1
    Yes. If you use some kind of DAO object, a good place to do this is in the constructor, and then make the DAO a singleton. – Olivier Michallat Nov 19 '14 at 11:12