Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Clarification on transaction isolation and management #424

Description

@cochiseruhulessin

Consider the following simulation of concurrent access:

# pylint: skip-file
import asyncio
import os

from databases import Database


async def tx1(db):
    async with db.transaction():
        await db.execute("INSERT INTO foo VALUES (1)")
        await asyncio.sleep(1.5)


async def tx2(db):
    async with db.transaction():
        await asyncio.sleep(0.5)
        result = await db.execute("SELECT * FROM foo")
        assert result is None, result
        await asyncio.sleep(1)


async def main():
    db = Database("postgresql://rdbms:rdbms@localhost")
    await db.connect()
    await db.execute("CREATE TABLE IF NOT EXISTS foo (bar int4)")
    await db.execute("TRUNCATE foo CASCADE")

    await asyncio.gather(
        tx1(db.connection()),
        tx2(db.connection())
    )


if __name__ == '__main__':
    asyncio.run(main())

This code should exit succesfully, but either fails with cannot perform operation: another operation is in progress (which is also weird because a new connection is requested) or at the assert statement. Please provide some clarification regarding the expected transactional behavior and isolation of this module.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    clean upRefinement to existing functionality

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions