Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions asyncpg/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ async def start(self):
await self._connection.execute(query)
except BaseException:
self._state = TransactionState.FAILED
if con._top_xact is self:
con._top_xact = None
raise
else:
self._state = TransactionState.STARTED
Expand Down
30 changes: 30 additions & 0 deletions tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0


from unittest import mock

import asyncpg

from asyncpg import _testbase as tb
Expand Down Expand Up @@ -180,6 +182,34 @@ async def test_transaction_within_manual_transaction(self):
self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())

async def test_transaction_failed_begin(self):
self.assertIsNone(self.con._top_xact)

tr = self.con.transaction()
error = asyncpg.PostgresConnectionError(
'Timed-out waiting to acquire database connection.'
)

with mock.patch.object(
type(self.con), 'execute', side_effect=error
) as execute:
with self.assertRaises(asyncpg.PostgresConnectionError) as caught:
await tr.start()

self.assertIs(caught.exception, error)
execute.assert_awaited_once_with('BEGIN;')

self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())

# The next transaction must be a real top-level one.
async with self.con.transaction():
self.assertTrue(self.con.is_in_transaction())
await self.con.execute('SELECT 1')

self.assertIsNone(self.con._top_xact)
self.assertFalse(self.con.is_in_transaction())

async def test_isolation_level(self):
await self.con.reset()
default_isolation = await self.con.fetchval(
Expand Down