nbsp; self.isRootTrans := false
else
begin
self.DBOperate.BeginTrans; //是根事务,就开始事务
self.isRootTrans := true;
self.DBOperate.isCommit := true; //初始化提交标志为要提交事务
end;
end;
end;
destructor TTS_DL.Destroy;
begin
if self.isNeedTrans then
begin
//如果是根事务,就按照投票结果进行事务提交或回滚
if self.isRootTrans then
begin
if self.DBOperate.isCommit then
self.DBOperate.CommitTrans
else
self.DBOperate.RollbackTrans;
end;
end;
inherited;
end;
procedure TTS_DL.SetAbort;
begin
self.DBOperate.isCommit := self.DBOperate.isCommit And false; //投票说要回滚
end;
procedure TTS_DL.SetComplete;
begin
self.DBOperate.isCommit := self.DBOperate.isCommit And true; //投票说要提交
end;
回到刚才的业务类Tthing和Tperson,这次都从TTS_DL类继承下来。
Tthing = class(TTS_DL);
Tperson = class(TTS_DL);
Tthing的删除代码该为如下:
procedure Tthing.Drop(const thing:String);
var
sqlString:String;
begin
sqlString := 删除的SQL语句;
try
self. DBOperate.Execute(sqlString);
self. DBOperate.SetComplete; //投票提交
except
self. DBOperate.SetAbort; //投票回滚
raise;
end;
end;
Tperson的删除代码如下:
procedure Tperson.Drop(const person:String);
var
sqlString:String;
thing:Tthing;
begin
sqlString := 删除的SQL语句;
thing := Tthing.Create(self. DBOperate,true); //TDBOperate_DL类型的DBOperate是作为参数传进去的,true代表需要事务。
Try
Try
Thing.Drop(person);
Self.DBOperate.Execute(sqlString);
self.DBOperate.SetComplete; //投票提交
except
self. DBOperate.SetAbort; &n