Skip to content

Determine if a transaction already exists #1644

Closed Answered by dvarrazzo
KaplanAlex asked this question in Q&A
Discussion options

You must be logged in to vote

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:

# No transaction despite the block
with conn:
    print(conn.info.transaction_status)
0

# Transaction after a statement
with conn:
    cur = conn.cursor()
    cur.execute("select 1")
    print(conn.info.transaction_status)
2

I suggest you to move to psycopg 3 which has a more friendly conn.transaction() block and the behaviour you expect:

import psycopg

conn = psycopg.connect("", autocommit=True)

print(conn.info.transaction_status)
TransactionStatus.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@dvarrazzo
Comment options

Answer selected by dvarrazzo
@KaplanAlex
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants