1

Background

My application randomly had read-only transaction errors, which after debugging, I realized that apparently the connection keeps the settings from the last created transaction.

Should this be expected behavior of Firedac/PostgreSQL or does it have the potential to be a bug?

Code

I was able to reproduce the behavior in a strongly simplified, isolated version of the code:

    program Project1;

    {$APPTYPE CONSOLE}
    {$R *.res}

    uses
      FireDAC.Comp.Client,
      FireDAC.DApt,
      FireDAC.Phys.PG,
      FireDAC.Phys.PGDef,
      FireDAC.Stan.Async,
      FireDAC.Stan.Def,
      FireDAC.Stan.Intf,
      System.SysUtils;
    
    var
      LConnectionDef: IFDStanConnectionDef;
      LQueryRO: TFDQuery;
      LQueryRW: TFDQuery;
      LTransactionRO: TFDTransaction;
      LTransactionRW: TFDTransaction;
      LConnection: TFDCustomConnection;
    begin
      try
        FDManager.DriverDefFileAutoLoad := False;
        FDManager.ConnectionDefs.AutoLoad := False;
        FDManager.ConnectionDefs.Clear;
    
        FDManager.ConnectionDefs.BeginWrite;
        try
          LConnectionDef := FDManager.ConnectionDefs.AddConnectionDef;
          LConnectionDef.Name := 'MY_DB';
    
          with TFDPhysPGConnectionDefParams(LConnectionDef.Params) do
          begin
            DriverID := 'PG';
            CharacterSet := TFDPGCharacterSet.csUTF8;
            Server := '127.0.0.1';
            Database := 'public';
            Port  := 11786;
            UserName := 'my_user';
            Password := '123456';
          end;
        finally
          FDManager.ConnectionDefs.EndWrite;
        end;
    
        LConnection := FDManager.AcquireConnection('MY_DB', '');
    
        LTransactionRO := TFDTransaction.Create(nil);
        LQueryRO := TFDQuery.Create(nil);
        try
          LTransactionRO.Connection := LConnection;
          LTransactionRO.Options.ReadOnly := True;
    
          LQueryRO.Connection := LConnection;
          LQueryRO.Transaction := LTransactionRO;
    
          LQueryRO.Open('SELECT * FROM MY_TABLE');
        finally
          LQueryRO.Free;
          LTransactionRO.Free;
        end;
    
        LTransactionRW := TFDTransaction.Create(nil);
        LQueryRW := TFDQuery.Create(nil);
        try
          LTransactionRW.Connection := LConnection;
          LTransactionRW.Options.ReadOnly := False;
    
          LQueryRW.Connection := LConnection;
          LQueryRW.Transaction := LTransactionRW;
    
          //ERROR: cannot execute UPDATE in a read-only transaction.
          LQueryRW.ExecSQL('UPDATE MY_TABLE SET COLUMN_1 = COLUMN_1 WHERE 1=1');
        finally
          LQueryRW.Free;
          LTransactionRW.Free;
        end;
    
        Writeln('OK');
        Readln;
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
    end.

Note 1: If I comment the line LTransactionRO.Options.ReadOnly := True;, the UPDATE SQL run without errors.

Note 2: I am testing in Delphi Alexandria

5
  • 1
    Why do you expect to be able to write to the DBM when you're explicitly saying it should be read-only? Why do you need to set it Read-Only? And I never see you start the transaction or do a Commit/Rollback. Also, in the docs it say "The component, at first, is target to InterBase and Firebird client application developers." If you want to use Transactions, there are (usually) no need to add the TFDTransaction component. You can start a transaction by calling the TFDConnection.StartTransaction, and then TFDConnection.Commit (or TFDConnection.Rollback if something went wrong).
    – Eirik A.
    Sep 21 at 9:35
  • Read more about transactions here -> docwiki.embarcadero.com/RADStudio/Alexandria/en/…
    – Eirik A.
    Sep 21 at 9:38
  • @EirikA. In my project, transactions are always started and finished, I removed everything I could in the example above for simplification. Same error happens when I do Commit/Rollback. I use RO transactions for reports (select only) and RW transactions in other situations.
    – dipold
    Sep 21 at 11:47
  • 1
    As you can read here: Currently, explicit transaction object assignment is supported only for InterBase and Firebird.. But even then it still looks like a bug because setting ReadOnly parameter of first transaction should have no effect on the second statement.
    – BrakNicku
    Sep 21 at 13:18
  • @BrakNicku Wow, now I see. I have always worked with Firebird and I was guided by the same logic to manage PostgreSQL transactions. I will have to redesign my connection layer.
    – dipold
    Sep 21 at 14:15

1 Answer 1

1

As explained in the documentation FireDAC.Stan.Option.TFDTxOptions.ReadOnly

Use the ReadOnly property values to notify a DBMS that a transaction only reads data, but does not modify it. The default value is False. Setting the ReadOnly property to True allows the DBMS to optimize resource usage.

the current transaction is not modified. better to specify explicitly StartTransaction , Commit, Rollback.

freeing the TFDQuery will not close automatically the transaction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.