8000 Fix wrong default transaction isolation level by fantix · Pull Request #622 · MagicStack/asyncpg · GitHub
[go: up one dir, main page]

Skip to content

Fix wrong default transaction isolation level #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
CRF: determine outer transaction isolation level
  • Loading branch information
fantix committed Sep 21, 2020
commit 3b6218dac0349f9b726333b295b99494e6dcd979
25 changes: 13 additions & 12 deletions asyncpg/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


import enum
import warnings

from . import connresource
from . import exceptions as apg_errors
Expand All @@ -21,6 +20,11 @@ class TransactionState(enum.Enum):


ISOLATION_LEVELS = {'read_committed', 'serializable', 'repeatable_read'}
ISOLATION_LEVELS_BY_VALUE = {
'read committed': 'read_committed',
'serializable': 'serializable',
'repeatable read': 'repeatable_read',
}


class Transaction(connresource.ConnectionResource):
Expand Down Expand Up @@ -111,20 +115,17 @@ async def start(self):
con._top_xact = self
else:
# Nested transaction block
top_xact = con._top_xact
if top_xact._isolation is None:
if self._isolation:
w = apg_errors.InterfaceWarning(
'nested transaction may have a different isolation '
'level: current {!r}, outer unknown'.format(
self._isolation))
warnings.warn(w)
elif self._isolation:
if self._isolation != top_xact._isolation:
if self._isolation:
top_xact_isolation = con._top_xact._isolation
if top_xact_isolation is None:
top_xact_isolation = ISOLATION_LEVELS_BY_VALUE[
await self._connection.fetchval(
'SHOW transaction_isolation;')]
if self._isolation != top_xact_isolation:
raise apg_errors.InterfaceError(
'nested transaction has a different isolation level: '
'current {!r} != outer {!r}'.format(
self._isolation, top_xact._isolation))
self._isolation, top_xact_isolation))
self._nested = True

if self._nested:
Expand Down
54 changes: 28 additions & 26 deletions tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,36 +211,38 @@ async def test_isolation_level(self):
await self.con.reset()

async def test_nested_isolation_level(self):
set_sql = 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL '
isolation_levels = {
None,
'read_committed',
'repeatable_read',
'serializable',
'read_committed': 'read committed',
'repeatable_read': 'repeatable read',
'serializable': 'serializable',
}
for inner in isolation_levels:
for outer in isolation_levels:
with self.subTest(outer=outer, inner=inner):
async with self.con.transaction(isolation=outer):
if outer and inner and outer != inner:
with self.assertRaisesRegex(
asyncpg.InterfaceError,
'current {!r} != outer {!r}'.format(
inner, outer
)
):
async with self.con.transaction(
isolation=inner,
for inner in [None] + list(isolation_levels):
for outer, outer_sql_level in isolation_levels.items():
for implicit in [False, True]:
with self.subTest(
implicit=implicit, outer=outer, inner=inner,
):
if implicit:
await self.con.execute(set_sql + outer_sql_level)
outer_level = None
else:
outer_level = outer

async with self.con.transaction(isolation=outer_level):
if inner and outer != inner:
with self.assertRaisesRegex(
asyncpg.InterfaceError,
'current {!r} != outer {!r}'.format(
inner, outer
)
):
pass
elif not outer and inner:
with self.assertWarnsRegex(
asyncpg.InterfaceWarning,
'current {!r}, outer unknown'.format(inner),
):
async with self.con.transaction(
isolation=inner,
):
pass
else:
async with self.con.transaction(
isolation=inner,
):
pass
else:
async with self.con.transaction(isolation=inner):
pass
0