多线程查询数据库,程序无响应 ( 积分: 100 )

  • 主题发起人 主题发起人 cchh
  • 开始时间 开始时间
C

cchh

Unregistered / Unconfirmed
GUEST, unregistred user!
各位:
为何在 qryInThread.Open;的时候,
程序会没有规律的无响应???
有时候执行好好的,有时候整个程序就死了,
cpu 0%,没任何响应


调用函数
function ProcessDataThread( dataThread : TDataThread ):Boolean;
begin
Result := TRUE;
dataThread.Resume;
while not dataThread.Terminateddo
begin
Application.ProcessMessages;
Sleep(5);
end;
end;

function OpenSQLCompleteDataSetPlus( SQLQuery : TSQLCompleteDataSetPlus;
IsSelect : Boolean ):Boolean;
var
SQLThread : TSQLCompleteDataSetPlusThread;
i : Integer;
begin
if( IsSelect )then
begin
SQLQuery.Close;
SQLThread := TSQLCompleteDataSetPlusThread.Create(SQLQuery);
SQLThread.isSelect := IsSelect;
Result := ProcessDataThread(SQLThread);
SQLQuery.FieldDefs.Assign(SQLThread.qryInThread.FieldDefs);
SQLQuery.CreateDataSet;
SQLQuery.LogChanges := False;
while not SQLThread.qryInThread.Eofdo
begin
SQLQuery.Insert;
for i:= 0 to SQLThread.qryInThread.FieldDefs.Count-1do
SQLQuery.Fields.Fields.Value := SQLThread.qryInThread.Fields.Fields.Value;
SQLQuery.Post;
SQLThread.qryInThread.Next;
end;
SQLQuery.LogChanges := True;
SQLQuery.First;
FreeAndNil(SQLThread);
end
else
begin
SQLQuery.Close;
SQLThread := TSQLCompleteDataSetPlusThread.Create(SQLQuery);
SQLThread.isSelect := IsSelect;
Result := ProcessDataThread(SQLThread);
FreeAndNil(SQLThread);
end;
end;

线程定义

TDataThread = class( TThread )
qryInThread : TSQLQuery;
conInThread : TSQLConnection;
Log : TProcessLogger;
isSelect : Boolean;
constructor Create;
destructor Destory;
procedure WriteStartLog;
procedure WriteEndLog;
procedure WriteError;
end;

TSQLCompleteDataSetPlusThread = class( TDataThread )
public
constructor Create( SQLQuery : TSQLCompleteDataSetPlus );
procedure Execute;
override;
end;

{ TSQLThread }
constructor TDataThread.Create;
begin
inherited Create(TRUE);
FreeOnTerminate := FALSE;
qryInThread := TSQLQuery.Create(nil);
conInThread := TSQLConnection.Create(nil);
Log := TProcessLogger.Create;
conInThread.SQLHourGlass := False;
isSelect := True;
end;

destructor TDataThread.Destory;
begin
qryInThread.Close;
conInThread.Connected := False;
while( conInThread.ConnectionState <> csStateClosed )do
;
FreeAndNil(qryInThread);
FreeAndNil(conInThread);
FreeAndNil(Log);
end;

procedure TDataThread.WriteEndLog;
begin
Log.EndLog;
end;

procedure TDataThread.WriteStartLog;
var
TheMsg : String;
begin
case qryInThread.SQLConnection.ConnectionState of
csStateClosed:TheMsg := 'close';
csStateOpen:TheMsg := 'open';
csStateConnecting:TheMsg := 'connecting';
csStateExecuting:TheMsg := 'executing';
csStateFetching:TheMsg := 'fetching';
csStateDisconnecting:TheMsg := 'disconnecting';
end;
Log.StartLog(TheMsg);
end;

procedure TDataThread.WriteError;
begin
LogErrorMessage('Thread Error');
end;

constructor TSQLCompleteDataSetPlusThread.Create(SQLQuery: TSQLCompleteDataSetPlus);
var
Backup : TSQLConnection;
begin
inherited Create;
Backup := SQLQuery.SQLConnectionPlus;
conInThread.DriverName := Backup.DriverName;
AssignBASXConnection(conInThread,FALSE);
conInThread.LoginPrompt := False;
conInThread.Connected := True;
qryInThread.Close;
qryInThread.SQLConnection := conInThread;
qryInThread.SQL.Assign(SQLQuery.SQL);
qryInThread.Params.AssignValues(SQLQuery.Params);
end;

procedure TSQLCompleteDataSetPlusThread.Execute;
begin
Try
Synchronize(WriteStartLog);
if isSelect then
qryInThread.Open;
else
qryInThread.ExecSQL;
Synchronize(WriteEndLog);
Except
on e:exceptiondo
Synchronize(WriteError);
end;
Terminate;
end;
 
使用的数据库是firebird
 
fixed,
The driver of firebirddo
es not support multithread if using local connection
Sign....
 
我覺得delphi中做數據庫方面程序開發,沒必要用多線程
很占CPU
 
可能是你打开的数据库比较大或者线程内外同时使用一个connection导致连接占线
 
一个线程使用一个connection
或者考虑 &quot;连接池&quot;
 
使用多线程是为了消除主程序的无响应等待,客户对这个很反感,说我们的程序freeze his laptop, 而且同时我们也希望用多线程加快数据库查询速度。
我们确实是一个线程一个connection的,我们打开的数据库确实很大,但是原因
不是这个,自从我改了firebird的连接模式,就再没出现过了
 
谢谢了,散分
 
后退
顶部