用Cassandra重新准备查询-在哪里初始化PreparedStatement

时间:2018-07-18 09:02:24

标签: java cassandra-3.0 datastax-java-driver

我正在使用Cassandra3.x。我有很多准备查询并简单执行它们的方法。一开始看起来很简单

query.append("SELECT * FROM alarms WHERE alarm_id=").append(alarmId);
query.append(" AND date<").append(date);
ResultSet resultSet = dataSource.executeQuery(query.toString());

可以,但是我找到了有关参数化查询的信息。 Properly about PreparedStatement

我想更改它,以便重构代码:

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

看起来工作正常。但是,经过几次测试,我发现了警告:

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<=?'

我应该如何处理?

根据post about my problem,我想知道应该在哪里以及如何初始化PreparedStatement?

0 个答案:

没有答案