diff --git a/asyncpg/cursor.py b/asyncpg/cursor.py index b4abeed1..467eaa2c 100644 --- a/asyncpg/cursor.py +++ b/asyncpg/cursor.py @@ -111,7 +111,7 @@ def _check_ready(self): raise exceptions.InterfaceError( 'cursor: the prepared statement is closed') - if not self._connection._top_xact: + if not self._connection.is_in_transaction(): raise exceptions.NoActiveSQLTransactionError( 'cursor cannot be created outside of a transaction') diff --git a/tests/test_cursor.py b/tests/test_cursor.py index ad446bc3..0d625560 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -109,6 +109,17 @@ async def test_cursor_01(self): 'cursor cannot be created.*transaction'): await st.cursor() + async def test_cursor_external_transaction(self): + await self.con.execute('BEGIN') + try: + st = await self.con.prepare('SELECT generate_series(0, 2)') + cur = await st.cursor() + self.assertEqual( + await cur.fetch(10), + [(0,), (1,), (2,)]) + finally: + await self.con.execute('ROLLBACK') + async def test_cursor_02(self): st = await self.con.prepare('SELECT generate_series(0, 20)') async with self.con.transaction():