为何数据库查询时候线程会停止?高手请

cchh 2007-05-30 08:34:21
各位:

为何在 qryInThread.Open;的时候,
程序会没有规律的无响应???





调用函数
function ProcessDataThread( dataThread : TDataThread ):Boolean;
begin
Result := TRUE;
dataThread.Resume;
while not dataThread.Terminated do
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.Eof do
begin
SQLQuery.Insert;
for i:= 0 to SQLThread.qryInThread.FieldDefs.Count-1 do
SQLQuery.Fields.Fields[i].Value := SQLThread.qryInThread.Fields.Fields[i].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:exception do
Synchronize(WriteError);
end;
Terminate;
end;

...全文
190 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cchh 2007-05-31
  • 打赏
  • 举报
回复
Fixed, the driver of firebird doesn't support multi-thread if using local connection

sign.
cchh 2007-05-30
  • 打赏
  • 举报
回复
不是,因为我的SQL是查询,其实从来不做update,insert,delete的
yi10000 2007-05-30
  • 打赏
  • 举报
回复
是不是表锁了

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧