Skip to content

Commit f87d49b

Browse files
committed
don't rollback when commit fails
1 parent f75dfa6 commit f87d49b

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

gino/dialects/asyncpg.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import time
44

55
import asyncpg
6-
from asyncpg.transaction import TransactionState
76
from sqlalchemy import util, exc, sql
87
from sqlalchemy.dialects.postgresql import ( # noqa: F401
98
ARRAY,
@@ -243,10 +242,6 @@ async def commit(self):
243242
await self._tx.commit()
244243

245244
async def rollback(self):
246-
# noinspection PyProtectedMember
247-
if self._tx._state in (TransactionState.FAILED,
248-
TransactionState.ROLLEDBACK):
249-
return
250245
await self._tx.rollback()
251246

252247

gino/transaction.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ async def __aexit__(self, ex_type, ex, ex_tb):
173173
try:
174174
await self._tx.commit()
175175
except Exception:
176-
await self._tx.rollback()
177176
raise
178177
else:
179178
await self._tx.rollback()

tests/test_transaction.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
from asyncpg.transaction import TransactionState
32

43
from .models import db, User, qsize
54

@@ -44,6 +43,9 @@ async def test_connection_ctx(bind, mocker):
4443
'asyncpg.transaction.Transaction.commit').side_effect = IndexError
4544
with pytest.raises(IndexError):
4645
await tx.__aexit__(None, None, None)
46+
# clean up, and to simulate commit failed
47+
mocker.stopall()
48+
await tx._tx.rollback()
4749
assert await get_name() == 'commit'
4850
assert await get_name() == 'commit'
4951

@@ -252,10 +254,12 @@ async def test_base_exception(engine):
252254
assert False, 'Should not reach here'
253255

254256

255-
async def test_rollback_failed_transaction(engine):
256-
# for transaction whose state is rolled back or failed, rollback() won't
257-
# have any effect and shouldn't throw exceptions
258-
for state in (TransactionState.ROLLEDBACK, TransactionState.FAILED):
259-
async with engine.transaction() as tx:
260-
tx._tx._tx._state = state
261-
tx.raise_rollback()
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)