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

时间:2019-02-03 14:00:24

标签: delphi firedac

我创建了一个vcl组件,该组件使用firedac访问PostgreSQL数据库。除查询跟踪之外,其他方法都无法正常工作。

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

在embarcadero文档之后,在TFDConnection之前创建了TFDMoniFlatFileClientLink,并将跟踪设置为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连接的开始/停止,而不记录 查询的执行。如果我将TFDConnection和TFDMoniFlatFileClientLink组件放在表单或数据模块上,则会按预期跟踪查询执行。

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

最诚挚的问候,罗纳德

1 个答案:

答案 0 :(得分:2)

我试图通过添加一个简单的FireDAC测试平台来重现您的问题 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                 $0100C390                >> Definition [Name=""]
  4361928750001 19:48:07.094 TFDConnectionDef                 $0100C390                     . Server=MAT430\ss2014
  4361928750001 19:48:07.094 TFDConnectionDef                 $0100C390                     . User_Name=sa
  4361928750001 19:48:07.094 TFDConnectionDef                 $0100C390                     . Password=*****
  4361928750001 19:48:07.094 TFDConnectionDef                 $0100C390                     . Database=MAtest
  4361928750001 19:48:07.094 TFDConnectionDef                 $0100C390                     . OSAuthent=No
  4361928750001 19:48:07.094 TFDConnectionDef                 $0100C390                     . DriverID=MSSQL
  4361928750001 19:48:07.094 TFDConnectionDef                 $0100C390                     . MonitorBy=FlatFile
  4361928750001 19:48:07.094 TFDConnectionDef                 $0100C390                << 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

然后是厨房水槽的其余部分。