adoconnection1接sql的服务器1
adoconnection2接sql的服务器2
现在的操作是先在服务器1里插入操作
然后在服务器1里更新操作两台服务器是在不同的地方的,那么我现在做的是要保证这两个操作要么全做,要么什么也不做,不能出现服务器1里插入了但服务器2里面没更新这种情况,
这怎么来写delphi事务
adoconnection2接sql的服务器2
现在的操作是先在服务器1里插入操作
然后在服务器1里更新操作两台服务器是在不同的地方的,那么我现在做的是要保证这两个操作要么全做,要么什么也不做,不能出现服务器1里插入了但服务器2里面没更新这种情况,
这怎么来写delphi事务
function G_BeginTran(const adocnn: TADOConnection): boolean;
begin
Result := FALSE;
try
if adocnn.InTransaction then
begin
adocnn.RollbackTrans;
Exit;
end;
adocnn.BeginTrans;
Result := TRUE;
except
end;
end;function G_CommitTran(const adocnn: TADOConnection): boolean;
begin
Result := FALSE;
try
if not adocnn.InTransaction then Exit;
adocnn.CommitTrans;
Result := TRUE;
except
G_RollTran(adocnn);
end;
end;function G_RollTran(const adocnn: TADOConnection): boolean;
begin
result := false;
try
if not adocnn.InTransaction then Exit;
adocnn.RollbackTrans;
result := true;
except
end;
end;-----------------------------------------------------------------------------
...
if not (G_BeginTran(adoconnection1) and G_BeginTran(adoconnection2)) then exit;
if not 服务器1里插入操作失败 then
begin
G_RollTran(adoconnection1);
G_RollTran(adoconnection2);
exit;
end;
if 服务器2里更新操作失败 then
begin
G_RollTran(adoconnection1);
G_RollTran(adoconnection2);
exit;
end;
if not (G_CommitTran(adoconnection1) and G_CommitTran(adoconnection2)) then
begin
G_RollTran(adoconnection1);
G_RollTran(adoconnection2);
exit;
end;
...