Is there an Oracle interface which tells you if the current transaction is the main transaction or an autonomous transaction withn PL/SQL context?
I know how to get the current transaction ID:
dbms_transaction.local_transaction_id
This would tell me if I'm within a transaction or not. But I need to further examine if the current transaction is the main transaction or if some code opened an autonomous transaction.
Use case / background:
During a test with utplsql there may be the need to differentiate between the main transaction and an autonomous transaction. For instance to see if the logic was correctly executed within an autonomous transaction. However, we are not sure yet about the test design in such cases so we want to see our options.
Surely, the autonomous transaction is only used during exception handling to create records which must not be rolled back.
When you start a PL/SQL block marked as autonomous, your current transaction pointer (
v$session.taddr) is NULL'd out even though the current transaction still exists and is still found inv$transaction. When you do your first DML operation within that autonomous transaction layer, you now have two transaction entries inv$transaction, but your transaction pointer (v$session.taddr) points to only the last/current one. That makes sense, asv$sessionwhich has only one row per session can only provide you with one transaction address for that one row, whereas you actually have two.Fortunately,
taddr/addris not the only link between these views. You also haveses_addr/saddrgoing the other way. So you can use this to test if there is another transaction you own but which is not the current one (the transaction addresses don't match):If you get something other than 0, you are in an autonomous transaction.
You will of course need the
SELECT ANY DICTIONARYpriv to see these views. Testing it out:Output: