Skip to content

Commit 25aaaf5

Browse files
authored
fix: allow cursors in external transactions (#1350)
1 parent 6a7fbd3 commit 25aaaf5

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎asyncpg/cursor.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def _check_ready(self):
111111
raise exceptions.InterfaceError(
112112
'cursor: the prepared statement is closed')
113113

114-
if not self._connection._top_xact:
114+
if not self._connection.is_in_transaction():
115115
raise exceptions.NoActiveSQLTransactionError(
116116
'cursor cannot be created outside of a transaction')
117117

‎tests/test_cursor.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ async def test_cursor_01(self):
109109
'cursor cannot be created.*transaction'):
110110
await st.cursor()
111111

112+
async def test_cursor_external_transaction(self):
113+
await self.con.execute('BEGIN')
114+
try:
115+
st = await self.con.prepare('SELECT generate_series(0, 2)')
116+
cur = await st.cursor()
117+
self.assertEqual(
118+
await cur.fetch(10),
119+
[(0,), (1,), (2,)])
120+
finally:
121+
await self.con.execute('ROLLBACK')
122+
112123
async def test_cursor_02(self):
113124
st = await self.con.prepare('SELECT generate_series(0, 20)')
114125
async with self.con.transaction():

0 commit comments

Comments
 (0)