multithreading - DBX 错误 : Driver could not be properly initialized when use OmniThreadLibrary (but ok otherwise)

原文 标签 multithreading delphi delphi-xe2 dbexpress

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

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

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.'.



连接是在每个线程中创建的,而不是共享数据模块:
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;

如果我直接执行这个,打开就ok了。如果我使用 Parallel.Async然后得到错误。这里发生了什么?

最佳答案

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

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.

关于multithreading - DBX 错误 : Driver could not be properly initialized when use OmniThreadLibrary (but ok otherwise),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9242890/

相关文章:

c++ - C++线程本地单例间歇性故障

delphi - 如何访问继承或继承的图像 Canvas ?

delphi - 我如何处理最大化的样式化窗口,这些窗口在相邻显示器上显示边框?

delphi - TRichEdit 控件中的分页符

delphi - XE2 中的 TDateTimePicker VCL 样式故障仅在 Windows 经典主题上发生

c++ - std::shared_ptr 的 use_count() 周围的完整内存屏障是否会使其成为可靠的计数器?

multithreading - 终止VC++中的线程

delphi - 超出范围的索引列表(0)Delphi

c# - 在C#.net中创建两个任务,并在特定时间段后关闭其中一个

delphi - 为什么不调用OnEndDrag事件?