2012-02-11 3 views
2

백그라운드에서 장기 실행 작업을 만들어야합니다. 나는 이것을 도울 수있는 OmniThreadLibrary 호핑을 사용하고 있습니다.DBX 오류 : 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; 

직접 실행하는 경우 확인을 엽니 다. Parallel.Async을 사용하면 오류가 발생합니다. 여기 무슨 일 이니?

답변

7

나는 여기에 대한 정보를 발견

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.