Reputation: 4776
I have a service application which connects to MSSQL using TADOConnection
and provides client applications with some data using Cromis.IPC
's TIPCServer
object. This service application might be installed on the same machine which is running MSSQL, and might start before MSSQL
service. So, I can not connect to ADO in OnServiceStart
event. I tried to connect to ADO in OnExecuteRequest
event of TIPCServer
, but since it is running in a separate thread, I got "CoInitialize has not been called". Then, I tried to synchronize this with my main thread like this:
TThread.Synchronize(nil, ConnectDB);
but it didn't work. My application stops responding right there.
Next, I modified the OnExecuteRequest
event to pass the thread which is calling it as a parameter, and I tried this:
AThread.Synchronize(AThread, ConnectDB);
But no change. When I turned on Use debug DCUs, I found out that my application stops responding on this line:
function WaitForSyncWaitObj(P: Pointer; Timeout: Cardinal): Integer;
begin
Result := WaitForSingleObject(THandle(P), Timeout); // <-- I'm stuck here...
end;
I'm completely lost. Any ideas?
PS: I didn't provide my code to prevent cluttering the question. If you need any part of it, just let me know.
PS 2: Before this, I tried to use a Timer in my service application to wait for a period before connecting to MSSQL. It seems like MSSQL starts pretty soon, even before network drivers are loaded (I have McAfee VSE 8.7 installed which replaces the network driver). My service connects to MSSQL, but then, when the network driver is loaded, the connection is broken. If there is a way to tell windows (XP and later) to start a service after network driver is loaded, my problem is solved. Any hint is really appreciated.
Upvotes: 1
Views: 355
Reputation: 125729
Call CoInitialize(nil);
in your TThread
constructor, and CoUnitialize
in it's destructor. Both of them are declared in the ComObj
unit; you may need to add it to your uses
clause in the unit containing the thread class.
Database connections are thread-specific (meaning each thread has to have it's own connection), and ADO
requires COM; that means that the thread has to initialize COM for itself, and should release it when the thread ends.
Upvotes: 1