Rollback raises not in Transaction

I try to insert an entity violating an unique key.
Postgres and RemoteDB are raising UK-violation as expected.

But Rollback in ObjectManager.SaveOrUpdate raises “not in Transaction”.

TObjectManager.SaveOrUpdate
...
      DoCommit(Transaction);
    except
      DoRollback(Transaction);
      raise;
    end;
end;

On Server Side TRemoteDatabase.Commit is called and set FTransaction := nil;
Trans.Commit is raising the UK-exception, so that TObjectManager is calling DoRollback.
DoRollback is leading to TRemoteDatabase.Rollback, that raises Exception.Create('Cannot rollback. Database is not in a transaction.'); because FTransaction has already been set to nil in Commit.

changing Commit like this would fix my issue, so that I get the expected UK-Exception:

procedure TRemoteDatabase.Commit;
begin
  if FTransaction <> nil then
  begin
    FTransaction.Commit;
    FTransaction := nil;
  end
  else
    raise Exception.Create('Cannot commit. Database is not in a transaction.');
end;

and because they look similar I also changed Rollback:

procedure TRemoteDatabase.Rollback;
begin
  if FTransaction <> nil then
  begin
    FTransaction.Rollback;
    FTransaction := nil;
  end
  else
    raise Exception.Create('Cannot rollback. Database is not in a transaction.');
end;

and maybe you could define an exception type for
Exception.Create('Cannot commit. Database is not in a transaction.');
so that we could fetch this and do some stuff like initialize connections.

Thanks for the report and suggestion. Changes will be applied in next release.

So I could leave my changes as is until you will publish next release?

Yes, correct.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.