View Issue Details

IDProjectCategoryView StatusLast Update
0038981FPCDatabasepublic2021-06-11 17:00
ReporterDarwin O'Connor Assigned ToMichael Van Canneyt  
PrioritynormalSeverityminorReproducibilityalways
Status assignedResolutionopen 
Platformx64OSGentoo Linux 
Product Version3.2.0 
Summary0038981: Executing previously prepared statment fails after commiting transactions for PostgreSQL
DescriptionIf you prepare a statement, and then start and commit a unrelated transaction, all the TPQTrans objects are freed, so an exception occurs when the prepared statement is run again.

It's possible I am miss using transactions.
Steps To Reproduce//This test program takes a database name as a parameter. The password may need to be filled in.
program floatparam;

uses sysutils, db, pqconnection, sqldb;

var
   Conn : TPQConnection;
   trans : TSQLTransaction;
   q : TSQLQuery;

begin
   Conn:=TPQConnection.create(nil);
   Conn.DatabaseName:=paramstr(1);
   Conn.UserName:='postgres';
   Conn.Password:='';
   Conn.open;
   trans:=TSQLTransaction.create(nil);
   trans.Database:=conn;
   conn.transaction:=trans;
   q:=TSQLQuery.create(nil);
   q.Database:=Conn;
   q.sql.Add('select :i');
   q.params[0].datatype:=ftinteger;
   q.prepare;
   q.params[0].asinteger:=1;
   q.open;
   writeln(q.fields[0].asinteger);
   q.close;
   if not trans.Active then trans.starttransaction;
   trans.commit;
   q.params[0].asinteger:=2;
   q.open; //EAccessViolation occurs here
   writeln(q.fields[0].asinteger);
   q.Close;
   q.free;
   trans.free;
   conn.free;
end.
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Activities

Michael Van Canneyt

2021-06-11 15:46

administrator   ~0131260

Your transaction is not unrelated.
It's the transaction q is connected to.
But of course it should not cause a crash, it should prepare the query again..

Darwin O'Connor

2021-06-11 17:00

reporter   ~0131261

I was able to fix it by creating a new transaction by adding this to TPQConnection.Execute

if FStatementType in [stInsert,stUpdate,stDelete,stSelect] then
      begin
      if tr=nil then begin
         if not aTransaction.Active then aTransaction.StartTransaction;
         TPQTrans(aTransaction.Handle).RegisterCursor(Cursor as TPQCursor);
      end;

Issue History

Date Modified Username Field Change
2021-06-10 01:51 Darwin O'Connor New Issue
2021-06-11 15:45 Michael Van Canneyt Assigned To => Michael Van Canneyt
2021-06-11 15:45 Michael Van Canneyt Status new => assigned
2021-06-11 15:46 Michael Van Canneyt Note Added: 0131260
2021-06-11 17:00 Darwin O'Connor Note Added: 0131261