Locktable and rollback
Posted: Thu 01 May 2014 23:47
Delphi XE2
MYDAC 7.6.12
I would appreciate some help with this problem
The program is attempting to apply a batch of data that the user has entered, and is stored in a ClientDataSet that I have called CDS. It needs to update a customer balance.
If I do not use locktable, everything works as expected, but If I use locktable, then if there is an error while processing, any previous edits to the customer file have been committed instead of rolled back.
Here is my code
Thanks for your help
MYDAC 7.6.12
I would appreciate some help with this problem
The program is attempting to apply a batch of data that the user has entered, and is stored in a ClientDataSet that I have called CDS. It needs to update a customer balance.
If I do not use locktable, everything works as expected, but If I use locktable, then if there is an error while processing, any previous edits to the customer file have been committed instead of rolled back.
Here is my code
Code: Select all
//--- Run Update process ---
MyConnect.Open;
CDS.First;
MyConnect.StartTransaction;
Try
TblClient.Open;
TblClient.IndexFieldNames := 'ID';
TblClient.LockTable(ltWrite); //<=========== LockTable
While not CDS.EOF do begin
try
if TblClient.Locate('ID',CDSID.AsInteger,[]) <> true then begin
raise Exception.Create('Could not find Client'); // data commited if locktable
exit;
end;
TblClient.Edit;
TblClientBal0.AsCurrency := TblClientBal0.AsCurrency + CDSAmt.AsCurrency;
TblClient.Post;
Except
if TblClient.State in [dsEdit, dsInsert] then TblClient.Cancel;
MyConnect.Rollback;
TblClient.UnLockTable;
Raise;
exit;
end;
CDS.Next;
end;
MyConnect.Commit;
Finally
TblClient.UnLockTable; //<======== UnlockTable
TblClient.Close;
End;
end;