以下代码纯属测试,写得有点乱,请高手帮我找问题。
毕业设计中遇到多线程与数据库问题。
怀疑多线程中动态生成的N个Tadoquery共享主Form1中的Tadoconnection时会有问题,于是写测试代码来测试,结果确实如此,但又想不通测试代码为什么出错,请各位高手帮忙看一下,付上源代码如下
//-----------主窗体放置一TADOconnection /Tmemo/Tbutton三个控件
//其中Tmemo用于显示从数据库中取得的时间
//Tbutton 用于生成n个的查询动作。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB,unit2;
type
TForm1 = class(TForm)
Button1: TButton;
ADOConnection1: TADOConnection;
//TadoConnection作为线程中动态生成
//TadoQuery的公用连接///
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
TestThread:TMyThread;
//创建n个一模一样的线程
TestThread2:tmythread;
//与上面的一模一样///
TEstThread3:Tmythread;
TestThread4:Tmythread;
TestThread5:Tmythread;
TEstThread6:Tmythread;
TestThread7:Tmythread;
TestThread8:Tmythread;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
//点击button1瞬间生成8个线程,动态生成8个adoquery,它们共享form1的
//adoconnection1,点击button1到第七次时出错了,每次都是这样的
TestThread:=TMyThread.Create(adoconnection1,memo1);
Testthread2:=TMyThread.Create(adoconnection1,memo1);
TEstThread3:=Tmythread.Create(adoconnection1,memo1);
TestThread4:=Tmythread.Create(adoconnection1,memo1);
TestThread5:=Tmythread.Create(adoconnection1,memo1);
TEstThread6:=Tmythread.Create(adoconnection1,memo1);
TestThread7:=Tmythread.Create(adoconnection1,memo1);
TestThread8:=Tmythread.Create(adoconnection1,memo1);
end;
end.
//----------------线程单元---------------------------//
unit Unit2;
interface
uses
Classes,DB,StdCtrls, ADODB,ActiveX;
type
TMyThread = class(TThread)
private
FAdoQuery:TADOQuery;
FMemory:Tmemo;
FADOConnection:TADOConnection;
protected
procedure Execute;
override;
public
constructor Create(adoconnection:TadoConnection;memory:TMemo);
destructor destroy;
procedure runsql;
end;
implementation
constructor TMyThread.Create(adoconnection: TadoConnection;
memory: TMemo);
begin
CoInitialize(nil);
//初始化ado
inherited create(false);
Fadoconnection:=adoconnection;
Fadoquery:=Tadoquery.Create(nil);
fadoquery.Connection:=fadoconnection;
Fmemory:=memory;
FreeOnTerminate:=True;
end;
destructor TMyThread.destroy;
begin
Fadoquery.Free;
CoUninitialize;
inherited destroy;
end;
procedure TMyThread.Execute;
begin
synchronize(runsql);
end;
procedure TMyThread.runsql;
var
datetimestr:string;
begin
datetimestr:= 'select to_char(sysdate,'''+'yyyymmdd HH24:MI:SS'''+') from dual';
if not Fadoconnection.Connected then
try
FadoConnection.Open;
except
Fmemory.Lines.Add('opendb error');
end;
try
////////------不要了 Fadoquery:=Tadoquery.Create(nil);
Fadoquery.Connection:=fadoconnection;
fadoquery.SQL.Clear ;
fadoquery.Close;
fadoquery.SQL.Add(datetimestr);
fadoquery.open ;
datetimestr:=fadoquery.Fields[0].AsString;
fmemory.Lines.Add(datetimestr);
//显示时间
except
Fmemory.Lines.Add('sql error');
end;
end;
end.
按button1第七次后出错,结果如下
project project1.exe
ora-01000:超出打出光标最大值。
Memo1
20050421 21:04:54
20050421 21:04:54
20050421 21:04:54
20050421 21:04:54
20050421 21:04:54
20050421 21:04:54
20050421 21:04:54
20050421 21:04:54
20050421 21:04:55
20050421 21:04:55
20050421 21:04:55
20050421 21:04:55
20050421 21:04:55
20050421 21:04:55
20050421 21:04:55
20050421 21:04:55
20050421 21:04:56
20050421 21:04:56
20050421 21:04:56
20050421 21:04:56
20050421 21:04:56
20050421 21:04:56
20050421 21:04:56
20050421 21:04:56
20050421 21:04:56
20050421 21:04:57
20050421 21:04:57
20050421 21:04:57
20050421 21:04:57
20050421 21:04:57
20050421 21:04:57
20050421 21:04:57
20050421 21:04:57
20050421 21:04:57
20050421 21:04:57
20050421 21:04:57
20050421 21:04:57
20050421 21:04:57
20050421 21:04:57
20050421 21:04:57
20050421 21:04:58
20050421 21:04:58
20050421 21:04:58
20050421 21:04:58
20050421 21:04:58
20050421 21:04:58
20050421 21:04:58
20050421 21:04:58
20050421 21:04:59
20050421 21:04:59
sql error
sql error
sql error
sql error
sql error
sql error