0

Here's my code:

@Override public void updateUser(String instance, String storeName, final String userId, final String newUsername, final String newPassword) { if (storeName == null || storeName == null) { return; } final PersistentEntityStore entityStore = PersistentEntityStores.newInstance(xodusRoot + instance); entityStore.executeInTransaction(new StoreTransactionalExecutable() { @Override public void execute(@NotNull final StoreTransaction txn) { EntityId roleEntityId = txn.toEntityId(userId); final Entity entity = txn.getEntity(roleEntityId); if(newUsername != null) { entity.setProperty("username", newUsername); } if(newPassword != null) { entity.setProperty("password", newPassword); } //txn.commit(); } }); entityStore.close(); } I want to know if for this code, is txn.commit(); required so the transaction to be executed, how about for rollback?

ps.

I wanted this code to return boolean if all transaction completed successfully but cannot find way except for txn.commit which returns boolean, is that the way? So it must be required?

quarks
  • 33,478
  • 73
  • 290
  • 513

1 Answers1

1

If you use methods like executeInTranction() or computeInTransaction(), then you should not call txn.commit(). Just use the executeInTranction() method to make sure the transaction is committed - if your program reached the next statement after executeInTranction then the transaction was committed.

Vyacheslav Lukianov
  • 1,913
  • 8
  • 12
  • Ok I see, so plainly setting the response to true at the end of the line of the execute will mean that transaction completed? Nice. – quarks Aug 14 '18 at 16:07
  • @xybrek you can use `computeInTransaction()` that returns `true` as a final statement, but it's the same as just use executeInTransaction and don't check the return value. – Vyacheslav Lukianov Aug 14 '18 at 16:15