这是我的代码:
@覆盖
公共(public)无效updateUser(字符串实例,字符串商店名称,最终字符串用户Id,
最终字符串新用户名,最终字符串新密码){
if (商店名称 == null || 商店名称 == null) {
返回;
}
最终 PersistentEntityStoreEntityStore = PersistentEntityStores.newInstance(xodusRoot + 实例);
EntityStore.executeInTransaction(new StoreTransactionalExecutable() {
@覆盖
公共(public)无效执行(@NotNull最终StoreTransaction txn){
EntityId 角色EntityId = txn.toEntityId(userId);
最终实体实体 = txn.getEntity(roleEntityId);
if(newUsername!= null) {
entity.setProperty("用户名", newUsername);
}
if(新密码!= null) {
entity.setProperty("密码", newPassword);
}
//txn.commit();
}
});
实体商店.close();
}
我想知道对于这段代码,是否需要 txn.commit();
以便执行事务,回滚怎么样?
ps。
如果所有事务都成功完成,但我希望此代码返回 boolean 值,但除了返回 boolean 值的 txn.commit
之外找不到其他方法,是这样吗?那么一定是需要的吗?
最佳答案
如果您使用 executeInTranction()
或 computeInTransaction()
等方法,则不应调用 txn.commit()
。只需使用 executeInTranction()
方法来确保事务已提交 - 如果您的程序在executeInTranction 之后到达下一条语句,则事务已提交。
关于java - Xodus 是否需要 txn.commit() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51844967/