【发布时间】:2016-08-05 00:05:48
【问题描述】:
我在关注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 是反模式,那么解决方案是什么?
【问题讨论】:
标签: cassandra cql datastax cql3 datastax-java-driver