我很难找到正确的方法来在内存中使用 FireDAC (CachedUpdates) 来获取 ApplyUpdates
方法上的错误。
这是我的场景,主从关系,由以下因素组成:
- 1
TFDConnection
- 2
TFDQuery
- 2
TDataSource
- 1
TFDSchemaAdapter
两个查询都设置为 CachedUpdates
并链接到 FDSchemaAdapter
。
FDQuery2
(详细信息)通过 MasterSource
属性与主数据库链接。 MasterFields
和 IndexFieldNames
设置为“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/