DBX 错误:使用 OmniThreadLibrary 时驱动程序无法正确初始化(但其他情况下正常)

发布于 2025-01-04 19:45:51 字数 1579 浏览 1 评论 0原文

我需要在后台执行一个长时间运行的任务。我正在使用 OmniThreadLibrary 希望这可以帮助我。

我使用 dbexpress+mssql 驱动程序。当位于主线程中时,我可以正常连接,但是得到:

Project Project1.exe 引发异常类 TDBXError 并带有消息 'DBX 错误:驱动程序无法正确初始化。客户端库 可能缺少、安装不正确、版本错误或 系统路径中可能缺少驱动程序。'.

连接是在每个线程中创建的,而不是共享数据模块:

type
  TdbManager = class(TObject)
  private
    { private declarations }
    FCon: TSQLConnection;
  public
    { public declarations }
    procedure Open(Driver:String; aparams:TStringList);overload;
    procedure Close;

    constructor Create;
    destructor Destroy;override;
  end;

  constructor TdbManager.Create;
begin
  inherited Create;
  FCon := TSQLConnection.Create(nil);
end;

procedure TdbManager.Open(Driver: String; aparams: TStringList);
var
  i: Integer;
  key:string;
begin
  FCon.DriverName := Driver;

  for i := 0 to params.Count - 1 do
  begin
    key := params.Names[i];
    FCon.Params.Values[key] := params.Values[key];
  end;

  LogMsg('Open DB '+ Driver + ': ' + FHost + '\' + FDatabase);

  FCon.Open;
  LogMsg('Done.');
end;

并且执行后台任务:

procedure TBackupPlan.OnScheduleTrigger(Sender: TScheduledEvent);
begin
  Parallel.Async(procedure
  begin
    ExecuteDataTask( Sender.Name );
  end);
end;

procedure TBackupPlan.ExecuteDataTask(const Name: String);
var
  db:TdbManager;
begin
  db := nil;

  db := TSqlServerManager.Create;
  db.Open(self.Driver, options);

  result := db;
end;

如果我直接执行此操作,则打开确定。如果我使用 Parallel.Async 则会出现错误。这里发生了什么?

I need to make a long running task in the background. I'm using OmniThreadLibrary hopping this could help me.

I use dbexpress+mssql driver. I can connect ok when is in the main thread, but get:

Project Project1.exe raised exception class TDBXError with message
'DBX Error: Driver could not be properly initialized. Client library
may be missing, not installed properly, of the wrong version, or the
driver may be missing from the system path.'.

The connections are created in each thread, not shared datamodule:

type
  TdbManager = class(TObject)
  private
    { private declarations }
    FCon: TSQLConnection;
  public
    { public declarations }
    procedure Open(Driver:String; aparams:TStringList);overload;
    procedure Close;

    constructor Create;
    destructor Destroy;override;
  end;

  constructor TdbManager.Create;
begin
  inherited Create;
  FCon := TSQLConnection.Create(nil);
end;

procedure TdbManager.Open(Driver: String; aparams: TStringList);
var
  i: Integer;
  key:string;
begin
  FCon.DriverName := Driver;

  for i := 0 to params.Count - 1 do
  begin
    key := params.Names[i];
    FCon.Params.Values[key] := params.Values[key];
  end;

  LogMsg('Open DB '+ Driver + ': ' + FHost + '\' + FDatabase);

  FCon.Open;
  LogMsg('Done.');
end;

And the background task is executed:

procedure TBackupPlan.OnScheduleTrigger(Sender: TScheduledEvent);
begin
  Parallel.Async(procedure
  begin
    ExecuteDataTask( Sender.Name );
  end);
end;

procedure TBackupPlan.ExecuteDataTask(const Name: String);
var
  db:TdbManager;
begin
  db := nil;

  db := TSqlServerManager.Create;
  db.Open(self.Driver, options);

  result := db;
end;

If I execute this directly, open ok. If I use Parallel.Async then get the error. What is happend here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温折酒 2025-01-11 19:45:51

我在这里找到了有关此内容的信息:

http://docwiki.embarcadero.com/RADStudio/en/DbExpress_Database_Specific_Information< /a>

MSSQL 驱动程序需要调用控制台应用程序和工作线程的 CoInitialize 和 CoUninitialize

MSSQL 驱动程序不调用 CoInitialize 或 CoUninitialize。早些时候
MSSQL 驱动程序的版本,它是一个 COM 驱动程序,称为
直接CoInitialize和CoUninitialize,这样不好
实践。 VCL 应用程序会为您处理这些调用,因此 VCL
应用程序不需要调用 CoInitialize 和 CoUninitialize。
但是,在控制台应用程序中使用 MSSQL 驱动程序的应用程序
或者在工作线程中需要调用CoInitialize/CoUninitialize。如果这个
如果未进行调用,您将看到以下错误消息:“DBX 错误:驱动程序无法正确初始化。客户端库可能是
缺少、未正确安装、版本错误或驱动程序
系统路径中可能丢失。”有关 CoInitialize 的帮助,请参阅
MSDN 上的 CoInitialize 函数。

I found information about this here:

http://docwiki.embarcadero.com/RADStudio/en/DbExpress_Database_Specific_Information

MSSQL Driver Requires Calls to CoInitialize and CoUninitialize for Console Applications and Worker Threads

The MSSQL driver does not call CoInitialize or CoUninitialize. Earlier
versions of the MSSQL driver, which is a COM driver, called
CoInitialize and CoUninitialize directly, which is not a good
practice. VCL applications take care of these calls for you, so VCL
applications do not require calling CoInitialize and CoUninitialize.
However, applications using the MSSQL driver in console applications
or in worker threads need to call CoInitialize/CoUninitialize. If this
call is not made, you will see the following error message: "DBX Error: Driver could not be properly initialized. Client library may be
missing, not installed properly, of the wrong version, or the driver
may be missing from the system path." For help on CoInitialize, see
the CoInitialize Function on MSDN.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文