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