Uniconnection, transaction, firebird

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
isysoftware
Posts: 44
Joined: Wed 14 Nov 2012 17:33

Uniconnection, transaction, firebird

Post by isysoftware » Tue 20 Aug 2013 16:35

Hi All,

I have 2 distinct projects in Delphi 2007 and unidac 5.0.2 connected to a firebird database.

Whit this code:

Code: Select all

with uniquery1 do
   begin
       close;
       sql.clear;
       sql.add('select * from table');
       open;
       edit;
       insert;
       fieldbyname('afield').asinteger:=1;
       try
          begin
              connection.starttransaction;
              post;
              connection.commit;
          end;
       excpet on e:exception do
           if(connection.intransaction) then
              connection.rollback
   end
I have an exception with message that says that there is an active transaction.
I have checked my code but I haven't executed any transaction: this code is the only in my Unit.

Any suggestions?

Thanks, Flavio

AndreyZ

Re: Uniconnection, transaction, firebird

Post by AndreyZ » Wed 21 Aug 2013 07:29

The point is that InterBase and Firebird require an active transaction for any operation under data. When you open a dataset without transaction, UniDAC starts transaction internally, and it allows you to read and write data. That is why, to avoid the problem, you should use two TUniTransaction components. One TUniTransaction will be used for reading data, another - for modifying data. Here is a code example:

Code: Select all

UniQuery1.Close;
UniQuery1.Transaction := UniTransaction1; // a transaction for reading data
UniQuery1.UpdateTransaction := UniTransaction2; // a transaction for modifying data
UniQuery1.SQL.Text := 'select * from tablename';
UniQuery1.Open;
UniQuery1.UpdateTransaction.StartTransaction;
try
  UniQuery1.Insert;
  UniQuery1.FieldByName('afield').AsInteger := 1;
  UniQuery1.Post;
  UniQuery1.UpdateTransaction.Commit;
except
  UniQuery1.UpdateTransaction.Rollback;
end;

Post Reply