How to post a record with a transaction that will rollback (PostgresSQL)

The forum for ZeosLib 7.2 Report problems. Ask for help, post proposals for the new version and Zeoslib 7.2 features here. This is a forum that will be edited once the 7.2.x version goes into RC/stable!!

My personal intention for 7.2 is to speed up the internals as optimal a possible for all IDE's. Hope you can help?! Have fun with testing 7.2
Post Reply
seu_madruga
Fresh Boarder
Fresh Boarder
Posts: 1
Joined: 07.07.2023, 03:29

How to post a record with a transaction that will rollback (PostgresSQL)

Post by seu_madruga »

Hello everyone.

I Have a transaction and I will execute a rollback, but I need to post a log table record.
This insert log record
Examle:

Code: Select all

var
  con : TZConnection;
  qry1, qry2, qry3, qryLog : TZQuery;
begin
  try
      con.TransactIsolationLevel := tiReadCommitted;
      con.StartTransaction;

      qry1.Connection := con;
      qry2.Connection := con;
      qry3.Connection := con;
      qryLog.Connection := con;
            

      // STEP 01
      qryLog.SQL.Text := 'insert into LOG'; // Log STEP 01
      qryLog.Execute; 
      
      qry1.SQL.Text := 'insert into XPTO1';
      qry1.Execute; // Maybe Error here    
          
      // STEP 02
      qryLog.SQL.Text := 'insert into LOG'; // Log STEP 02
      qryLog.Execute; 
      
      qry2.SQL.Text := 'insert into XPTO2';
      qry2.Execute; // Maybe Error here    
                  
      // STEP 03
      qryLog.SQL.Text := 'insert into LOG'; // Log STEP 03
      qryLog.Execute; 
      
      qry3.SQL.Text := 'insert into XPTO3;
      qry3.Execute; // Maybe Error here
            
      con.Commit;
    except             
      con.Rollback; // I need to cancel qry1, qry2 and qry3, but I need to post qryLog
    end;
  
Thanks
marsupilami
Platinum Boarder
Platinum Boarder
Posts: 1918
Joined: 17.01.2011, 14:17

Re: How to post a record with a transaction that will rollback (PostgresSQL)

Post by marsupilami »

You could think about inserting in the log when the transaction is finished:

Code: Select all

var
  con : TZConnection;
  qry1, qry2, qry3, qryLog : TZQuery;
begin
  try
      con.TransactIsolationLevel := tiReadCommitted;
      con.StartTransaction;

      qry1.Connection := con;
      qry2.Connection := con;
      qry3.Connection := con;
      qryLog.Connection := con;
            

      // STEP 01
      qryLog.SQL.Text := 'insert into LOG'; // Log STEP 01
      qryLog.Execute; 
      
      qry1.SQL.Text := 'insert into XPTO1';
      qry1.Execute; // Maybe Error here    
          
      // STEP 02
      qryLog.SQL.Text := 'insert into LOG'; // Log STEP 02
      qryLog.Execute; 
      
      qry2.SQL.Text := 'insert into XPTO2';
      qry2.Execute; // Maybe Error here    
                        
      qry3.SQL.Text := 'insert into XPTO3;
      qry3.Execute; // Maybe Error here
            
      con.Commit;
    except             
      con.Rollback; // I need to cancel qry1, qry2 and qry3, but I need to post qryLog
    end;

    // STEP 03
    qryLog.SQL.Text := 'insert into LOG'; // Log STEP 03
    qryLog.Execute; 
  
Fr0sT
Zeos Dev Team
Zeos Dev Team
Posts: 280
Joined: 08.05.2014, 12:08

Re: How to post a record with a transaction that will rollback (PostgresSQL)

Post by Fr0sT »

The most correct way is 2nd independent transaction. IDK about Postgres, maybe it would require second connection.
Post Reply