Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions tests/test_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,34 @@ async def test_transaction_rollback(database_url):
assert len(results) == 0


@pytest.mark.parametrize("database_url", DATABASE_URLS)
@mysql_versions
@async_adapter
async def test_transaction_doesnt_leak_when_commit_fails(database_url):
"""
Ensure that transaction doesn't leak the connection when the commit or rollback
fails
"""

async with Database(database_url) as database:
with pytest.raises(Exception) as excinfo:
async with database.connection() as connection:
await connection.execute(
"""
CREATE TABLE test (id integer PRIMARY KEY INITIALLY DEFERRED);
"""
)
async with connection.transaction():
await connection.execute("insert into test (id) values (1)")
await connection.execute("insert into test (id) values (1)")

# During transaction.commit() postgres will raise this exception:
# asyncpg.exceptions.UniqueViolationError: duplicate key value violates unique constraint "test_pkey"
# DETAIL: Key (id)=(1) already exists.
assert "unique constraint" in str(excinfo.value)
assert connection._connection_counter == 0
Comment on lines +554 to +570
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aminalaee I think this test it's only valid for postgresql. Is there a way to skip for other databases?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sure,

async def test_transaction_commit_serializable(database_url):



@pytest.mark.parametrize("database_url", DATABASE_URLS)
@mysql_versions
@async_adapter
Expand Down