Delphi + FireDAC 在ApplyUpdates 上获取数据库错误

标签 delphi transactions firebird master-detail firedac

我很难找到正确的方法来在内存中使用 FireDAC (CachedUpdates) 来获取 ApplyUpdates 方法上的错误。

这是我的场景,主从关系,由以下因素组成:

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

两个查询都设置为 CachedUpdates 并链接到 FDSchemaAdapterFDQuery2(详细信息)通过 MasterSource 属性与主数据库链接。 MasterFieldsIndexFieldNames 设置为“idMaster”。还检查了属性 FetchOptions.DetailCascade

我还有一个按钮来执行应用:

  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;

到目前为止一切正常。

当我的数据库抛出异常(例如违反约束)时,就会出现问题。异常(exception)情况是不引发。因此,我的事务没有“回滚”。

ps:我使用的是Delphi XE7和Firebird 2.5

最佳答案

作为documentation状态:

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.

所以......您不应该期待异常,但您应该检查ApplyUpdates返回的值来决定是否可以提交或处理错误。

关于Delphi + FireDAC 在ApplyUpdates 上获取数据库错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43102165/

相关文章:

sql - 使用 firebird 数据库在 SQL 中包含空格的别名

sql - 如何在 "when any" block 中获取与 Firebird SQL 异常关联的文本?

delphi - 如何处理第三方库中的警告/提示?

Delphi - 使用 RTTI e Addr 获取相同的字段指针

java - java EE 中的事务是什么?

oracle - ADO.NET,在没有事先提交或回滚的情况下关闭 OracleConnection : Will it leak?

c# - DataTable.Load(FbDataReader) 不会将所有内容加载到 DataTable

Delphi 'AND' 评估有 2 个条件

delphi - 调整显示 Gamma 值

django - 如何测试具有 transaction.atomic(using=myDb) 的 Django View ?