我关注this link to use a batch transaction without using BATCH keyword。
Cluster cluster = Cluster.builder()
.addContactPoint(“127.0.0.1")
.build();
Session session = cluster.newSession();
//Save off the prepared statement you're going to use
PreparedStatement statement = session.prepare(“INSERT INTO tester.users (userID, firstName, lastName) VALUES (?,?,?)”);
//
List<ResultSetFuture> futures = new ArrayList<ResultSetFuture>();
for (int i = 0; i < 1000; i++) {
//please bind with whatever actually useful data you're importing
BoundStatement bind = statement.bind(i, “John”, “Tester”);
ResultSetFuture resultSetFuture = session.executeAsync(bind);
futures.add(resultSetFuture);
}
//not returning anything useful but makes sure everything has completed before you exit the thread.
for(ResultSetFuture future: futures){
future.getUninterruptibly();
}
cluster.close();
我的问题是,给定的方法是可以从不同的表中插入,更新或删除数据 ,如果其中任何一个失败都应该通过保持相同的性能而失败(如链路)。
使用这种方法我试过,我试图插入,删除不同表中的数据,一个查询失败,所以以前的查询都执行并更新了数据库。
使用BATCH
我可以看到,如果任何语句失败,则所有语句都将失败。但是在不同的表上使用BATCH
是反模式的,那么解决方案是什么?
答案 0 :(得分:1)
使用BATCH我可以看到,如果任何语句失败,则所有语句都将失败。
错误,LOGGED BATCH的保证是:如果批处理中的某些语句失败,将重试它们直到成功。
但是在不同的表上使用BATCH是反模式的,那么解决方案是什么?
Cassandra不可能进行ACID交易,它需要某种全局锁定或全局协调,并且在性能方面是令人望而却步的。
但是,如果您不关心性能成本,可以使用轻量级事务原语实现您自己的全局锁定/租用系统,如here所述
但要准备好面对糟糕的表现