Page 1 of 1

how to rollback an transaction

Posted: Thu 03 Sep 2009 14:13
by jkuiper
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.

Posted: Thu 03 Sep 2009 15:54
by swierzbicki
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;