How to handle transactions properly with Zeos?

The offical for ZeosLib 7.3 Report problems, ask for help, post proposals for the new version of Zeoslib 7.3/v8
Quick Info:
-We made two new drivers: odbc(raw and unicode version) and oledb
-GUID domain/field-defined support for FB
-extended error infos of Firebird
-performance ups are still in queue
In future some more feature will arrive, so stay tuned and don't hassitate to help
Kirill
Fresh Boarder
Fresh Boarder
Posts: 11
Joined: 17.08.2020, 14:37

Re: How to handle transactions properly with Zeos?

Post by Kirill »

EgonHugeist wrote: 11.07.2020, 07:13 TZTransaction added on 7.3-testing.
Thanks for adding TZTransaction component. Can you give an example of the proper use of this component?
User avatar
EgonHugeist
Zeos Project Manager
Zeos Project Manager
Posts: 1936
Joined: 31.03.2011, 22:38

Re: How to handle transactions properly with Zeos?

Post by EgonHugeist »

@Krill,
still not had time to write totorials. IMHO it should work like the FPC/FireDac/Unidac transaction components.
Did you test it?

Side notes: If you're using the component, avoid the Conenction [StartTransaction/Commit/Rollback] and use the TZTransaction component instead.
Was this helpfull?
Best regards, Michael

You want to help? http://zeoslib.sourceforge.net/viewtopic.php?f=4&t=3671
You found a (possible) bug? Use the new bugtracker dude! http://sourceforge.net/p/zeoslib/tickets/

Image
Kirill
Fresh Boarder
Fresh Boarder
Posts: 11
Joined: 17.08.2020, 14:37

Re: How to handle transactions properly with Zeos?

Post by Kirill »

Hi. I use Lazarus 2.0.10 with FPC 3.2.0. Zeos version is r7078-trunk. DBMS is Firebird 3.0. There are three components on the form: ZConnection1, ZQuery1, ZTransaction1. ZTransaction1.Connection is ZConnection1. ZQuery1.Connection is ZConnection1. ZQuery1.Transaction is ZTransaction1. The source code is given below.

Code: Select all

procedure TForm1.ButtonTransactionClick(Sender: TObject);
var
  i: integer;
begin
  ZConnection1.AutoCommit := False;
  ZTransaction1.AutoCommit := False;

  ZQuery1.SQL.Text := 'insert into TestTable(A) values (1)';
  ZConnection1.Connected := True;

  ZTransaction1.StartTransaction();
  for i := 1 to 10 do
  begin
    ZQuery1.ExecSQL();
  end;

  //  Invalid operation in AutoCommit mode
  if RadioGroup1.ItemIndex = 0 then
  begin
    ZTransaction1.Commit();
  end
  else
  begin
    ZTransaction1.Rollback();
  end;

  ZConnection1.Connected := False;
end;
There is an error «Invalid operation in AutoCommit mode» for both commit and rollback. What am I doing wrong?
User avatar
EgonHugeist
Zeos Project Manager
Zeos Project Manager
Posts: 1936
Joined: 31.03.2011, 22:38

Re: How to handle transactions properly with Zeos?

Post by EgonHugeist »

Hello Krill,

i fixed a bug: https://sourceforge.net/p/zeoslib/code-0/7088/

and added a TestCase:

Code: Select all

procedure TZGenericecTransactionTestCase.TestQueryTransaction;
var Query: TZQuery;
    Transaction: TZTransaction;
    Succeeded: Boolean;
begin
  Query := CreateQuery;
  Check(Query <> nil);
  Transaction := CreateTransaction;
  try
    Check(Transaction.AutoCommit, 'the txn should be in autocommit mode');
    Connection.Connect;
    Transaction.TransactIsolationLevel := Connection.DbcConnection.GetMetadata.GetDatabaseInfo.GetDefaultTransactionIsolation;
    Query.Transaction := Transaction;
    //this test should pass if the txn is in autocommit mode
    Query.SQL.Text := 'DELETE FROM equipment where eq_id = ' + SysUtils.IntToStr(TEST_ROW_ID);
    Query.ExecSQL;
    Transaction.StartTransaction;
    Succeeded := False;
    with Query do try
      CheckFalse(Transaction.AutoCommit);
      { Create prepared statement for equipment table }
      Sql.Text := 'INSERT INTO equipment (eq_id, eq_name, eq_type, eq_cost, eq_date, '
          + ' woff_date) VALUES(:q_id, :eq_name, :eq_type, :eq_cost, :eq_date, :woff_date)';
      CheckEquals(6, Params.Count);

      Params[5].DataType := ftDate;

      Params[0].AsInteger := TEST_ROW_ID;
      Params[1].AsString := '\xyz\'+#13;
      Params[2].AsInteger := 7;
      Params[3].AsFloat := 1234.567;
      Params[4].AsDateTime := EncodeDate(1999, 8, 5);
      Params[5].Value := Null;
      ExecSQL;

      CheckEquals(1, RowsAffected);
      Succeeded := True;
    finally
      if Succeeded
      then Transaction.Commit
      else Transaction.Rollback;
    end;
  finally
    FreeAndNil(Transaction);
    FreeAndNil(Query);
    Connection.ExecuteDirect('DELETE FROM equipment where eq_id = ' + SysUtils.IntToStr(TEST_ROW_ID));
  end;
end;

It shows how i do interpret the use of the new component. Any feedback would be welcome, but please open a new thread for it.
Best regards, Michael

You want to help? http://zeoslib.sourceforge.net/viewtopic.php?f=4&t=3671
You found a (possible) bug? Use the new bugtracker dude! http://sourceforge.net/p/zeoslib/tickets/

Image
Kirill
Fresh Boarder
Fresh Boarder
Posts: 11
Joined: 17.08.2020, 14:37

Re: How to handle transactions properly with Zeos?

Post by Kirill »

Hi, Michael!

The problem with TZTransaction component has been solved. Thank you very much.

Kirill.
Post Reply