FireDAC.Comp.Client.TFDCustomTransaction.StartTransaction

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

procedure StartTransaction;

C++

void __fastcall StartTransaction();

Properties

Type Visibility Source Unit Parent
procedure
function
public
FireDAC.Comp.Client.pas
FireDAC.Comp.Client.hpp
FireDAC.Comp.Client TFDCustomTransaction

Description

Starts a new transaction at the DBMS.

Call StartTransaction to start a new transaction at the DBMS. 

FireDAC supports nested transactions. If DBMS does not support nested transactions, FireDAC will emulate them using savepoints. If a transaction is already active, FireDAC will put a savepoint, otherwise it will start a new transaction. Use Options.EnableNested to enable / disable nested transactions.   Before calling StartTransaction, an application may adjust the settings of the Options property as desired. Adjusting options after the transaction is started does not affect the current transaction. 

All data modifications, such as INSERT / UPDATE / DELETE, made after a call to StartTransaction, may be either confirmed by calling Commit, or undone by calling Rollback.  Some DBMSs will fail to start the transaction if there are active result sets (for example, MS SQL Server 2005).

Example

procedure TForm1.ChangeButtonClick(Sender: TObject);
begin
  FDQuery1.Transaction := FDTransaction1;
  FDQuery1.SQL.Text := 'select * from employees';

  FDQuery2.Transaction := FDTransaction1;
  FDQuery2.SQL.Text := 'update employees set salary = salary * :k where id = :id';

  FDTransaction1.Options.Isolation := xiRepeatableRead;
  FDTransaction1.StartTransaction;
  try
    FDQuery1.Open;
    while not FDQuery1.Eof do begin
      FDQuery2.ExecSQL('', [1.2, FDQuery1.FieldByName('id').AsInteger]);
      FDTransaction1.CommitRetaining;
      FDQuery1.Next;
    end;
    FDTransaction1.Commit;
  except
    FDTransaction1.Rollback;
    raise;
  end;
end;

See Also