1
votes

I'm facing difficulty to find a correct way to get errors on ApplyUpdates method, using FireDAC in memory (CachedUpdates).

Here is my scenario, a master-detail relationship, compounded by:

  • 1 TFDConnection
  • 2 TFDQuery
  • 2 TDataSource
  • 1 TFDSchemaAdapter

Both queries are setted as CachedUpdates and are linked to FDSchemaAdapter. The FDQuery2 (detail) is linked with the master by MasterSource property. MasterFields and IndexFieldNames are setted as "idMaster". The property FetchOptions.DetailCascade is also checked.

I also have a button to perform the apply:

  try
    FDConnection1.StartTransaction;

    FDSchemaAdapter1.ApplyUpdates(0);

    FDQuery1.CommitUpdates;
    FDQuery2.CommitUpdates;

    FDConnection1.Commit;
  except on E: Exception do
    begin
      FDConnection1.Rollback;
      raise Exception.CreateFmt('Something went wrong. Error: %s', [E.Message]);
    end;
  end;

Everything is working fine so far.

The problem occurs when my database throw an exception, such a constraint violation. The exception is not raising. Consequently, my transaction is not 'rollbacked'.

ps: I'm using Delphi XE7 and Firebird 2.5

1
Possibly the exception is being caught and silenced somewhere. To find out: Make sure Notify on language exceptions is checked under Tools | Options | Debugger Options and that use debug dcus is checked under Project | Options. Run the app to where you expect the exception to occur and see if the debugger breaks execution. If it does, use View | Debug windows | Call stack to locate it. - MartynA
@MartynA the Applyupdates exception is fired only in debug mode. I have already tried using OnReconcileError event, but with no success - R.P Silveira

1 Answers

3
votes

As the documentation states:

ApplyUpdates returns the number of errors it encountered. Based on this return value and the setting of AMaxErrors successfully, applied updates are removed from the centralized change log. If the update process is aborted before all updates are applied, any unapplied updates remain in the change log.

ApplyUpdates does not raise an exception. Instead, the application should review erroneous records using Reconcile and the OnReconcileRow event handler or the FilterChanges and RowError properties for each dataset. For more details, read "Reviewing errors" at Caching Updates.

So... you should not be expecting an exception but you should be checking the value returned by ApplyUpdates to decide if you can commit or handle errors.