不能在手动或分布事务方式下创建新的连接。 ( 积分: 100 )

  • 主题发起人 主题发起人 DragonInCity
  • 开始时间 开始时间
D

DragonInCity

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, Grids, DBGrids, DB, ADODB;
type
TMyThread=class(TThread)
protected
procedure Execute;override;
end;

TForm1 = class(TForm)
ADOCN: TADOConnection;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
DBGrid2: TDBGrid;
DataSource2: TDataSource;
ADOQuery2: TADOQuery;
Splitter1: TSplitter;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
Label1: TLabel;
procedure BitBtn1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure BitBtn2Click(Sender: TObject);
private
ThreadSwitch:Boolean;
MyThread:TMyThread;
ErrorStr:TStringList;
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
ADOCN.begin
Trans;
try
ADOQuery2.Edit;
ADOQuery2.FieldByName('Updatetime').AsDateTime:=Date;
ADOQuery2.UpdateBatch;
ADOCN.CommitTrans;
except
ADOCN.RollbackTrans;
Raise Exception.Create(Exception(ExceptObject).Message);
end;

if ThreadSwitch then
begin
ThreadSwitch:=false;
MyThread:=TMyThread.Create(false);
end;
end;

{ TMyThread }
procedure TMyThread.Execute;
begin
inherited;
FreeOnTerminate:=true;
try
Form1.ADOQuery1.Close;
Form1.ADOQuery1.Open;
Form1.Label1.Caption:=IntToStr(Form1.ADOQuery1.RecordCount)+'条';
except
Form1.Label1.Caption:='';
Form1.Label1.Repaint;
Form1.ThreadSwitch:=true;
Form1.ErrorStr.Add(inttostr(Form1.ErrorStr.Count)+':'+Exception(ExceptObject).Message);
Raise Exception.Create(Exception(ExceptObject).Message);
end;
Form1.ThreadSwitch:=true;
end;

程序在事务提交后马上创建一个线程,但有时会出现“不能在手动或分布事务方式下创建新的连接。”的错误,不明白什么原因,但如果去掉事务就没问题了,请大侠帮忙解决一下!!!
 
每个线程要访问COM库都要初始化,主线程由其它单元完成了这一步。
代码修改为以下那样
procedure TMyThread.Execute;
begin
ActiveX.CoInitialize(nil);
try
.... ....原代码
finally
ActiveX.CoUninitialize();
end;
end
 

Similar threads

后退
顶部