I am using Cassandra 3.x. I have a lot of methods where I prepare query and simple execute them. At the very start it looks simple
query.append("SELECT * FROM alarms WHERE alarm_id=").append(alarmId);
query.append(" AND date<").append(date);
ResultSet resultSet = dataSource.executeQuery(query.toString());
It was okay but I found information about parameterized queries. Properly about PreparedStatement
I wanted to change it so I refactor code:
query.append("SELECT * FROM alarms WHERE alarm_id=? AND date<?");
PreparedStatement pS = dataSource.getSession().prepare(query.toString());
BoundStatement bS = pS.bind().setInt(0, alarmId).setDate(1, date);
ResultSet rS = dataSource.session.execute(bS);
It looked like it worked fine. However after couple of tests I found WARN:
Re-preparing already prepared query is generally an anti-pattern and will likely affect performance. Consider preparing the statement only once. Query='SELECT * FROM alarms WHERE alarm_id=? AND date<=?'
How should I deal with it?
According to post about my problem I wonder where and how should I initialize PreparedStatement?