Determine if a transaction already exists #1644
-
Is there a way to tell when I'm already in a transaction created by the connection context manager while autocommit is set to True? I have a method which starts a transaction using the connection context manager. This method is called by another method which starts a transaction. I want to update my inner method to only start a transaction if it doesn't exist already. To do so the method needs to know if it's in a transaction. All fields I can find appear to be the same before and during the context manager block
Output
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You can use |
Beta Was this translation helpful? Give feedback.
Ah, you are using
get_transaction_status()
which returns the same info.What happens is that the transaction is started but not at the beginning of the block; rather at the first statement, like what happens for a non-autocommit transaction:
I suggest you to move to psycopg 3 which has a more friendly
conn.transaction()
block and the behaviour you expect: