diff --git a/asyncpg/transaction.py b/asyncpg/transaction.py index 562811e6..5ecb33af 100644 --- a/asyncpg/transaction.py +++ b/asyncpg/transaction.py @@ -184,12 +184,19 @@ async def __commit(self): query = 'COMMIT;' try: - await self._connection.execute(query) + result = await self._connection.execute(query) except BaseException: self._state = TransactionState.FAILED raise - else: - self._state = TransactionState.COMMITTED + + # An aborted transaction cannot be committed. PostgreSQL completes + # COMMIT with the command tag ROLLBACK and discards the changes. + if not self._nested and result == 'ROLLBACK': + self._state = TransactionState.ROLLEDBACK + raise apg_errors.InterfaceError( + 'cannot commit; the server rolled the transaction back') + + self._state = TransactionState.COMMITTED async def __rollback(self): self.__check_state('rollback') @@ -212,7 +219,11 @@ async def __rollback(self): @connresource.guarded async def commit(self): - """Exit the transaction or savepoint block and commit changes.""" + """Exit the transaction or savepoint block and commit changes. + + Raises ``InterfaceError`` if the server answers ``COMMIT`` with + ``ROLLBACK`` because an earlier statement failed. + """ if self._managed: raise apg_errors.InterfaceError( 'cannot manually commit from within an `async with` block') diff --git a/tests/test_prepare.py b/tests/test_prepare.py index 661021bd..77004aa0 100644 --- a/tests/test_prepare.py +++ b/tests/test_prepare.py @@ -451,11 +451,13 @@ async def check_simple(): # Try a cursor/timeout combination. Cursors should always use # named prepared statements. - async with self.con.transaction(): - with self.assertRaises(asyncio.TimeoutError): - async for _ in self.con.cursor( # NOQA - 'select pg_sleep(10)', timeout=0.1): - pass + with self.assertRaisesRegex( + asyncpg.InterfaceError, 'rolled the transaction back'): + async with self.con.transaction(): + with self.assertRaises(asyncio.TimeoutError): + async for _ in self.con.cursor( # NOQA + 'select pg_sleep(10)', timeout=0.1): + pass # Check that we can run queries after a failed cursor # operation. diff --git a/tests/test_timeout.py b/tests/test_timeout.py index 0bc8de8f..5a1d6a79 100644 --- a/tests/test_timeout.py +++ b/tests/test_timeout.py @@ -58,11 +58,13 @@ async def test_timeout_04(self): self.assertEqual(await self.con.fetch('select 1'), [(1,)]) st = await self.con.prepare('select pg_sleep(10)', timeout=0.1) - async with self.con.transaction(): - cur = await st.cursor() - with self.assertRaises(asyncio.TimeoutError), \ - self.assertRunUnder(MAX_RUNTIME): - await cur.fetch(1, timeout=0.1) + with self.assertRaisesRegex( + asyncpg.InterfaceError, 'rolled the transaction back'): + async with self.con.transaction(): + cur = await st.cursor() + with self.assertRaises(asyncio.TimeoutError), \ + self.assertRunUnder(MAX_RUNTIME): + await cur.fetch(1, timeout=0.1) self.assertEqual(await self.con.fetch('select 1'), [(1,)]) async def test_timeout_05(self): @@ -75,40 +77,50 @@ async def test_timeout_05(self): self.assertEqual(await self.con.fetch('SELECT 1'), [(1,)]) async def test_timeout_06(self): - async with self.con.transaction(): - with self.assertRaises(asyncio.TimeoutError), \ - self.assertRunUnder(MAX_RUNTIME): - async for _ in self.con.cursor( # NOQA - 'select pg_sleep(10)', timeout=0.1): - pass + with self.assertRaisesRegex( + asyncpg.InterfaceError, 'rolled the transaction back'): + async with self.con.transaction(): + with self.assertRaises(asyncio.TimeoutError), \ + self.assertRunUnder(MAX_RUNTIME): + async for _ in self.con.cursor( # NOQA + 'select pg_sleep(10)', timeout=0.1): + pass self.assertEqual(await self.con.fetch('select 1'), [(1,)]) - async with self.con.transaction(): - cur = await self.con.cursor('select pg_sleep(10)') - with self.assertRaises(asyncio.TimeoutError), \ - self.assertRunUnder(MAX_RUNTIME): - await cur.fetch(1, timeout=0.1) + with self.assertRaisesRegex( + asyncpg.InterfaceError, 'rolled the transaction back'): + async with self.con.transaction(): + cur = await self.con.cursor('select pg_sleep(10)') + with self.assertRaises(asyncio.TimeoutError), \ + self.assertRunUnder(MAX_RUNTIME): + await cur.fetch(1, timeout=0.1) - async with self.con.transaction(): - cur = await self.con.cursor('select pg_sleep(10)') - with self.assertRaises(asyncio.TimeoutError), \ - self.assertRunUnder(MAX_RUNTIME): - await cur.forward(1, timeout=1e-10) + with self.assertRaisesRegex( + asyncpg.InterfaceError, 'rolled the transaction back'): + async with self.con.transaction(): + cur = await self.con.cursor('select pg_sleep(10)') + with self.assertRaises(asyncio.TimeoutError), \ + self.assertRunUnder(MAX_RUNTIME): + await cur.forward(1, timeout=1e-10) - async with self.con.transaction(): - cur = await self.con.cursor('select pg_sleep(10)') - with self.assertRaises(asyncio.TimeoutError), \ - self.assertRunUnder(MAX_RUNTIME): - await cur.fetchrow(timeout=0.1) + with self.assertRaisesRegex( + asyncpg.InterfaceError, 'rolled the transaction back'): + async with self.con.transaction(): + cur = await self.con.cursor('select pg_sleep(10)') + with self.assertRaises(asyncio.TimeoutError), \ + self.assertRunUnder(MAX_RUNTIME): + await cur.fetchrow(timeout=0.1) - async with self.con.transaction(): - cur = await self.con.cursor('select pg_sleep(10)') - with self.assertRaises(asyncio.TimeoutError), \ - self.assertRunUnder(MAX_RUNTIME): - await cur.fetchrow(timeout=0.1) + with self.assertRaisesRegex( + asyncpg.InterfaceError, 'rolled the transaction back'): + async with self.con.transaction(): + cur = await self.con.cursor('select pg_sleep(10)') + with self.assertRaises(asyncio.TimeoutError), \ + self.assertRunUnder(MAX_RUNTIME): + await cur.fetchrow(timeout=0.1) - with self.assertRaises(asyncpg.InFailedSQLTransactionError): - await cur.fetch(1) + with self.assertRaises(asyncpg.InFailedSQLTransactionError): + await cur.fetch(1) self.assertEqual(await self.con.fetch('select 1'), [(1,)]) diff --git a/tests/test_transaction.py b/tests/test_transaction.py index f84cf7c0..0510b84a 100644 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -159,6 +159,67 @@ async def test_transaction_interface_errors(self): async with tr: pass + async def test_transaction_commit_when_server_rolls_back(self): + await self.con.execute(''' + CREATE TABLE commit_rb (name text); + ''') + try: + tr = self.con.transaction() + await tr.start() + await self.con.execute( + "INSERT INTO commit_rb (name) VALUES ('explicit')") + with self.assertRaises(asyncpg.DivisionByZeroError): + await self.con.execute('SELECT 1 / 0') + + with self.assertRaisesRegex( + asyncpg.InterfaceError, + 'rolled the transaction back'): + await tr.commit() + + self.assertTrue(repr(tr).startswith( + '