【发布时间】:2014-07-02 23:09:36
【问题描述】:
我在使用 Datastax java 驱动程序结合 BatchStatement 和轻量级事务时遇到了困难。
考虑以下几点:
String batch =
"BEGIN BATCH "
+ "Update mykeyspace.mytable set record_version = 102 where id = '" + id + "' if record_version = 101;
" <additional batched statements>
+ "APPLY BATCH";
Row row = session.execute(batch).one();
if (! row.getBool("[applied]")) {
throw new RuntimeException("Optimistic Lock Failure!");
}
这按预期运行,并指示我的轻量级事务是否成功并且我的批处理是否已应用。一切都很好。但是,如果我使用 BatchStatement 尝试相同的事情,我会遇到几个问题:
-- 我的轻量级事务“if”子句被忽略,并且始终应用更新
-- "Row" 结果为空,无法执行最后的 row.getBool("[applied]") 检查。
String update = "Update mykeyspace.mytable set record_version = ? where id = ? if record_version = ?";
PreparedStatement pStmt = getSession().prepare(update);
BatchStatement batch = new BatchStatement();
batch.add(new BoundStatement(pStmt).bind(newVersion, id, oldVersion));
Row row = session.execute(batch).one(); <------ Row is null!
if (! row.getBool("[applied]")) {
throw new RuntimeException("Optimistic Lock Failure!");
}
我做错了吗?或者这是 datastax BatchStatement 的限制?
【问题讨论】: