Skip to content

How to run a query outside of the transaction that has been started? #818

Answered by plaa
plaa asked this question in Q&A
Discussion options

You must be logged in to vote

Was able to make it work using the Manual Control for transactions, and wrap it up in a context manager:

@asynccontextmanager
async def start_isolated_db_transaction() -> AsyncIterator[Any]:
    """Start a database transaction that is isolated from any enclosing transaction.
    That is, the queries within this transaction are committed as soon as this context
    manager exits, even if there is an enclosing transation."""
    # This acquires a new connection from the Gino engine connection pool and
    # starts a transaction using it.  Implicit queries work correctly within it.
    async with db.bind.acquire() as connection:
        tx = await connection.transaction()
        try:
      …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by plaa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant