Search code examples
javaxodus

Deleting multiple keys -- can it be transactional?


Here is our code:

@Override
public boolean delete(String instance, final String storeName, 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));
            }
        }
    });
    env.close();
    return isSuccess[0];
}

I have two question for this.

  • Is this transactional, since this function is for deleting multiple keys, would this work like if one key fails to delete the other keys will not be deleted. Like all or nothing?
  • If in case within the txn an exception happened due to some reason, like key or storeName being null, how should that be handled? Or it does not matter since if there is an exception the transcation would fail and roll back automatically?

Solution

  • Xodus is an ACID-compliant transactional database. Among other things it means that mutations of data in transactions are consistent. In your case either all specified keys (transaction is committed) or no keys (transaction aborted) would be deleted. If a transaction is interrupted for some reason (exception, system/hardware failure) nothing is modified and the transaction automatically rolls back.