Skip to content

Commit ba60163

Browse files
committed
Merge branch 'failed-trans'
2 parents 3ddccda + b31db8d commit ba60163

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

gino/transaction.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,10 @@ async def __aenter__(self):
166166
return self
167167

168168
async def __aexit__(self, ex_type, ex, ex_tb):
169-
try:
170-
is_break = ex_type is _Break
171-
if is_break and ex.commit:
172-
ex_type = None
173-
if ex_type is None:
174-
await self._tx.commit()
175-
else:
176-
await self._tx.rollback()
177-
except Exception:
169+
is_break = ex_type is _Break
170+
if is_break and ex.commit or ex_type is None:
171+
await self._tx.commit()
172+
else:
178173
await self._tx.rollback()
179-
raise
180174
if is_break and ex.tx is self:
181175
return True

tests/test_transaction.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ async def test_connection_ctx(bind, mocker):
4343
'asyncpg.transaction.Transaction.commit').side_effect = IndexError
4444
with pytest.raises(IndexError):
4545
await tx.__aexit__(None, None, None)
46+
# clean up, and to simulate commit failed
47+
mocker.stopall()
48+
await tx._tx.rollback()
4649
assert await get_name() == 'commit'
4750
assert await get_name() == 'commit'
4851

@@ -249,3 +252,14 @@ async def test_base_exception(engine):
249252
except Exception:
250253
assert False, 'Should not reach here'
251254
assert False, 'Should not reach here'
255+
256+
257+
async def test_no_rollback_on_commit_fail(engine, mocker):
258+
mocker.patch(
259+
'asyncpg.transaction.Transaction.commit').side_effect = IndexError
260+
async with engine.acquire() as conn:
261+
tx = await conn.transaction().__aenter__()
262+
rollback = mocker.patch.object(tx._tx, 'rollback')
263+
with pytest.raises(IndexError):
264+
await tx.__aexit__(None, None, None)
265+
assert not rollback.called

0 commit comments

Comments
 (0)