我现在主要怀疑我的多线程的ADO访问有问题,我贴出代码,那位老大帮我看看:
TTestPool = class(TThread)
private
FConnectString:string;
AC:TADOConnection;
AQ:TADOQuery;
AP:TADOStoredProc;
{ Private declarations }
//初始化ADO数据库连接
function InitADO:boolean;
//释放ADO数据库连接
procedure FreeADO();
protected
procedure Execute; override;
public
constructor Create(CreateSuspended: Boolean;AConnectStr:string);
end;
implementation
{ TTestPool }
constructor TTestPool.Create(CreateSuspended: Boolean;
AConnectStr: string);
begin
FConnectString:=AConnectStr;
//CoInitializeEx(nil,COINIT_MULTITHREADED);
//CoInitialize(nil);
//InitADO();
inherited Create(CreateSuspended);
end;
procedure TTestPool.Execute;
begin
CoInitializeEx(nil,COINIT_MULTITHREADED);
InitADO();
try
while not Terminated do
begin
Sleep(1000);
end;
finally
FreeADO();
CoUninitialize();
end;
end;
procedure TTestPool.FreeADO;
begin
if AP <> nil then
begin
AP.Close;
AP.Free;
AP:=nil;
end;
if AQ <> nil then
begin
AQ.Close;
AQ.Free;
AQ:=nil;
end;
if AC <> nil then
begin
AC.Close;
AC.Free;
AC:=nil;
end;
end;
function TTestPool.InitADO: boolean;
begin
try
AC:=TADOConnection.Create(nil);
AQ:=TADOQuery.Create(nil);
AP:=TADOStoredProc.Create(nil);
AQ.Connection:=AC;
AP.Connection:=AC;
AC.ConnectionString:=FConnectString;
AC.Open();
Result:=True;
except
if AQ <> nil then
begin
AQ.Free;
end;
if AP <> nil then
begin
AP.Free;
end;
if AC <> nil then
begin
AC.Free;
end;
AC:=nil;
AQ:=nil;
AP:=nil;
Result:=False;
end;
end;