how to rollback an transaction

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
jkuiper
Posts: 138
Joined: Fri 04 Aug 2006 14:17

how to rollback an transaction

Post by jkuiper » Thu 03 Sep 2009 14:13

As far as I know this code is using an transaction:

Code: Select all

  with FKostenQuery do
  begin
    Connection.StartTransaction;
    Edit;
    FieldByName('bedrag_uitgerekend').AsFloat := ubedrag;
    FieldByName('bedrag_gewijzigd').AsFloat := ubedrag;
    Post;
    Connection.Commit;
  end;
But when the commit failed, you have to rollback the transaction.
How will I do this in code? The table will be opened, updated and closed in a class.

swierzbicki
Posts: 451
Joined: Wed 19 Jan 2005 09:59

Post by swierzbicki » Thu 03 Sep 2009 15:54

Check this ;) :

Code: Select all

  with FKostenQuery do 
  begin 
    Connection.StartTransaction; 
    Try
       Edit; 
       FieldByName('bedrag_uitgerekend').AsFloat := ubedrag; 
       FieldByName('bedrag_gewijzigd').AsFloat := ubedrag; 
       Post; 
       Connection.Commit; 
    Except
      On E: Exception do
      Begin
         Connection.Rollback;
         Showmessage(Format('An error occured: %s',[e.message]));
      end;
    End;
  end; 

Post Reply