put 和 delete 都可以在 Xodus 中进行交易吗?
Can both put and delete be transaction in Xodus?
这是我们代码中的批处理方法:
@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];
}
请问这段代码是怎么事务的,delete和put都可以事务吗?这个方法的目的是先删除现有的键,然后用不同的值重新放入,目的是它应该是事务,所以在循环中任何删除或添加失败,它应该回滚。这段代码已经这样做了吗?
是的,事务要么提交所有更改,要么什么都不提交。此外,您不必在为键添加新值之前删除键。
这是我们代码中的批处理方法:
@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];
}
请问这段代码是怎么事务的,delete和put都可以事务吗?这个方法的目的是先删除现有的键,然后用不同的值重新放入,目的是它应该是事务,所以在循环中任何删除或添加失败,它应该回滚。这段代码已经这样做了吗?
是的,事务要么提交所有更改,要么什么都不提交。此外,您不必在为键添加新值之前删除键。