Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Idea: add a no_transaction() decorator? #1086

@dvarrazzo

Description

@dvarrazzo

Sometime on a non-autocommit connection there is a need to run some statements outside a transaction. For example, in psycopg pool:

if conn.autocommit:
conn.execute("")
else:
conn.autocommit = True
try:
conn.execute("")
finally:
conn.autocommit = False

Maybe we could have a context manager on the connection to disable the transaction behaviour temporarily?

with conn.no_transaction():
    conn.execute(query)

Entering the block would fail if there is a transaction in progress. The connection would be put in autocommit mode into the block and reset to the original state on exit, even in case of exception. Unlike the transaction() block, there would be no return from the function call.

@asqui what do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions