Page 1 of 1

Locktable and rollback

Posted: Thu 01 May 2014 23:47
by softsmith
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

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;
Thanks for your help

Re: Locktable and rollback

Posted: Wed 07 May 2014 12:11
by PavloP
The correct way to use LOCK TABLES with transactional tables is to begin a transaction with SET autocommit = 0 and not START TRANSACTION. It is a specificity of MySQL server and we can't influence it.
You can find more detailed information in the MySQL documentation:
http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html

Re: Locktable and rollback

Posted: Thu 08 May 2014 12:39
by softsmith
Thank you. I appreciate your help.

Re: Locktable and rollback

Posted: Thu 08 May 2014 13:54
by PavloP
Feel free to contact us if you have any further questions.