Delphi FireDAC 查询执行跟踪不适用于组件创建的连接对象

Delphi FireDAC query execution tracing not working with connection object created by component

我创建了一个 vcl 组件,它使用 firedac 来访问 PostgreSQL 数据库。这工作正常,除了查询跟踪,我无法开始工作。

在组件构造函数中,我创建连接对象,设置参数等。在uses子句中,我添加了FireDAC.Phys.PG和FireDAC.Moni.Base,FireDAC.Moni.FlatFile。

根据 embarcadero 文档,在 TFDConnection 之前创建了 TFDMoniFlatFileClientLink,并将 tracing 设置为 true 对于 FDTracer 和 FDConnection.ConnectionIntf。

unit My.Database;

interface

uses
  System.SysUtils, System.Classes, FireDAC.Comp.UI, FireDAC.Comp.Client, FireDAC.Phys.PG,
  FireDAC.Moni.Base, FireDAC.Moni.FlatFile;

type
  TMyDatabase = class(TComponent)
  private
    { Private declarations }
    FDTracer: TFDMoniFlatFileClientLink;
    FDConnection: TFDConnection;
    FConnectionString: string;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function IsConnected: Boolean;
    property ConnectionString: stringread FConnectionString write FConnectionString;
    function OpenConnection: Boolean;
    function GetAsDataset(ASQL: string): TFDQuery;
  end;

implementation

uses
  FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Error, FireDAC.Stan.Def, FireDAC.Stan.Async,
  FireDAC.VCLUI.Login, FireDAC.VCLUI.Wait;

constructor TMyDatabase.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  FDTracer := TFDMoniFlatFileClientLink.Create(Self);
  FDConnection := TFDConnection.Create(Self);

  FDTracer.tracing := True;  

  with FDConnection do
  begin
    DriverName := 'PG';
    LoginPrompt := False;

    //params for PostgreSQL
    with Params do
    begin
      Add('Port=5432');
      Add('LoginTimeout=2');
      Add('Pooled=False');
      Add('OidAsBlob=No');
      Add('UnknownFormat=BYTEA');
      Add('ExtendedMetadata=True');
    end;
  end;

  FDConnection.ConnectionIntf.Tracing := True;
end;

function TMyDatabase.IsConnected: Boolean;
begin
  Result := FDConnection.Connected;
end;

function TMyDatabase.OpenConnection: Boolean;
begin
  with FDConnection do
  try
    ConnectionString := FConnectionString;

    if not Connected then
    begin
      Open;
    end;
  finally
    Result := Connected;
  end;
end;

function TMyDatabase.GetAsDataset(ASQL: string): TFDQuery;
begin
  Result := TFDQuery.Create(Self);

  with Result do
  begin
    Connection := FDConnection;
  end;

  Result.Open(ASQL);
end;

当我连接到数据库并通过 GetAsDataSet 执行查询时,跟踪文件仅记录 FireDac 连接的 starting/stopping,但不记录 查询的执行。如果我将 TFDConnection 和 TFDMoniFlatFileClientLink 组件放在表单或数据模块上,将按预期跟踪查询执行。

有人知道如何使用从组件动态创建的连接对象跟踪查询的执行吗?

此致,罗纳德

我试图在我的一个简单的 FireDAC 测试台上重现你的问题,方法是添加 a TFDMoniFlatFileClientLink 并得到类似的结果,即跟踪文件 包含的条目说明它已启动和停止,但两者之间没有任何内容。

正在查看http://docwiki.embarcadero.com/CodeExamples/Tokyo/en/FireDAC.MoniLayerClients_Sample 我似乎错过了一个小而重要的步骤,即设置 MonitorBy 参数 我的 FDConnection 为 'FlatFile',以匹配 TFDMoniFlatFileClientLink

所以,就在我的代码中打开连接之前,我在下面添加了第一行

  FDConnection1.Params.MonitorBy := mbFlatFile;
  FDConnection1.Connected := True;

然后跟踪 运行 很好。

部分跟踪提取

  --- new start of FireDAC Trace ---
  4361928750001 19:48:07.092 -=#!!! FireDAC Tracer started !!!#=-
  4361928750001 19:48:07.094 TFDPhysMSSQLConnection           TFDPhysMSSQLConnecti  . CreateConnection [ConnectionDef=""]
  4361928750001 19:48:07.094 TFDPhysMSSQLConnection           TFDPhysMSSQLConnecti  . CreateTransaction [ConnectionDef=""]
  4361928750001 19:48:07.094 TFDPhysMSSQLConnection           FDConnection1        >> Open [ConnectionDef=""]
  4361928750001 19:48:07.094 TFDConnectionDef                 00C390                >> Definition [Name=""]
  4361928750001 19:48:07.094 TFDConnectionDef                 00C390                     . Server=MAT430\ss2014
  4361928750001 19:48:07.094 TFDConnectionDef                 00C390                     . User_Name=sa
  4361928750001 19:48:07.094 TFDConnectionDef                 00C390                     . Password=*****
  4361928750001 19:48:07.094 TFDConnectionDef                 00C390                     . Database=MAtest
  4361928750001 19:48:07.094 TFDConnectionDef                 00C390                     . OSAuthent=No
  4361928750001 19:48:07.094 TFDConnectionDef                 00C390                     . DriverID=MSSQL
  4361928750001 19:48:07.094 TFDConnectionDef                 00C390                     . MonitorBy=FlatFile
  4361928750001 19:48:07.094 TFDConnectionDef                 00C390                << Definition [Name=""]
  4361928750001 19:48:07.094 TFDPhysMSSQLConnection           FDConnection1            >> FireDAC info
  4361928750001 19:48:07.094 TFDPhysMSSQLConnection           FDConnection1                 . Tool=RAD Studio 10.2
  4361928750001 19:48:07.094 TFDPhysMSSQLConnection           FDConnection1                 . FireDAC=16.0.0 (Build 88974)
  4361928750001 19:48:07.094 TFDPhysMSSQLConnection           FDConnection1                 . Platform=Windows 32 bit
  4361928750001 19:48:07.094 TFDPhysMSSQLConnection           FDConnection1                 . Defines=FireDAC_NOLOCALE_META;FireDAC_MONITOR
  4361928750001 19:48:07.094 TFDPhysMSSQLConnection           FDConnection1            << FireDAC info
  4361931710001 19:48:07.390 TFDPhysMSSQLConnection           FDConnection1             . SQLDriverConnect [szConnStr="DRIVER=SQL Server Native Client 11.0;UID=sa;PWD=*******;Server=MAT430\ss2014;Database=MAtest;Trusted_Connection=No;MARS_Connection=yes"]
  4361931710001 19:48:07.392 TFDPhysMSSQLDriver               MSSQL                     . ENTER SQLDriverConnectW
      HDBC                0x01044B18
      HWND                0x00000000
      WCHAR *             0x61942440 [      -3] "******\ 0"
      SWORD                       -3
      WCHAR *             0x61942440
      SWORD                       -3
      SWORD *             0x00000000
      UWORD                        0 <SQL_DRIVER_NOPROMPT>

  4361935310001 19:48:07.756 TFDPhysMSSQLDriver               MSSQL                     . EXIT  SQLDriverConnectW  with return code 1 (SQL_SUCCESS_WITH_INFO)
      HDBC                0x01044B18
      HWND                0x00000000
      WCHAR *             0x61942440 [      -3] "******\ 0"
      SWORD                       -3
      WCHAR *             0x61942440 <Invalid buffer length!> [-3]
      SWORD                       -3
      SWORD *             0x00000000
      UWORD                        0 <SQL_DRIVER_NOPROMPT>

      DIAG [01000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Changed database context to 'MAtest'. (5701)

      DIAG [01000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Changed language setting to us_english. (5703)

  4361935310001 19:48:07.758 TFDPhysMSSQLDriver               MSSQL                     . ENTER SQLGetDiagRecW
      SQLSMALLINT                  2 <SQL_HANDLE_DBC>
      SQLHANDLE           0x01044B18
      SQLSMALLINT                  1
      SQLWCHAR *          0x00FCB5CC
      SQLINTEGER *        0x0019F840
      SQLWCHAR *          0x0019D83A
      SQLSMALLINT               4096
      SQLSMALLINT *       0x0019F846

接着是厨房水槽的剩余部分。