这是我们代码中的批处理方法:
@Override
public boolean batchPutDelete(String instance, final String storeName, final Map<String, String> properties, final String... keys) {
final Boolean[] isSuccess = {false};
final List<String> keyList = Arrays.asList(keys);
final Environment env = Environments.newInstance(xodusRoot + instance);
env.executeInTransaction(new TransactionalExecutable() {
@Override
public void execute(@NotNull final Transaction txn) {
final Store store = env.openStore(storeName, StoreConfig.WITHOUT_DUPLICATES, txn);
for (String key : keyList) {
isSuccess[0] = store.delete(txn, StringBinding.stringToEntry(key));
}
Iterator<String> it = properties.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
String string = properties.get(key);
isSuccess[0] = store.put(txn, StringBinding.stringToEntry(key), StringBinding.stringToEntry(string));
}
}
});
env.close();
return isSuccess[0];
}
我想问一下这段代码是如何交易的,可以删除也可以交易吗?此方法的目的是先删除现有键,然后再将其重新放入不同的值,其目的是要进行事务处理,因此任何删除或添加操作都会在循环中失败,因此应回滚。这段代码已经做到了吗?
答案 0 :(得分:2)
是的,事务将要么提交所有更改,要么不提交任何更改。另外,您不必在为键添加新值之前删除键。