Search code examples
firebirdfirebird2.5firebird-psql

Firebird 2.5 exception handling within autonomous transaction


I'm experiencing performance drop in one of our Firebird stored procedures and I have no clue why. I have found the following code in the mentioned SP:

declare v_dummy integer;
...
in autonomous transaction do
begin
  -- insert may fail, but that is not a problem because it means the record is already there
  insert into my_table(my_field) values (:input_param);
when ANY do
  v_dummy = 1;
end

I see few dozens of records in RDB$TRANSACTIONS table with STATE 3, no relevant records in MON$TRANSACTIONS table.

The question is, if the insert fails will the autonomous transaction be rolled back or does the "when ANY do" prevent the rollback and there will be an opened transaction? Can I just remove the exception handling, so it will be rolled back automatically without raising an exception and blocking the rest of the code?


Solution

  • Using a when any do inside an autonomous transaction block will not rollback the transaction, instead it will commit once the block ends because the exception does not escape the block.

    However, this is probably the desired result: committing transactions in Firebird is (relatively) cheaper than rolling back. In fact, if a transaction rolls back when nothing was changed, Firebird will convert a rollback into a commit anyway.

    I don't think this is the cause of your performance problem, but without reproducible example, it is hard to reason about this.

    As an aside, transactions with state 3 are rolled back, and rolled back transactions have ended. MON$TRANSACTIONS only shows active transactions, so rolled back transactions will not be shown in that virtual table.