Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 69cdccf

Browse files
authored
Revert #328 parallel transactions (#472)
1 parent 3fbe526 commit 69cdccf

File tree

2 files changed

+8
-43
lines changed

2 files changed

+8
-43
lines changed

databases/core.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
from databases.interfaces import DatabaseBackend, Record
1515

1616
if sys.version_info >= (3, 7): # pragma: no cover
17-
import contextvars as contextvars
17+
from contextvars import ContextVar
1818
else: # pragma: no cover
19-
import aiocontextvars as contextvars
19+
from aiocontextvars import ContextVar
2020

2121
try: # pragma: no cover
2222
import click
@@ -69,9 +69,7 @@ def __init__(
6969
self._backend = backend_cls(self.url, **self.options)
7070

7171
# Connections are stored as task-local state.
72-
self._connection_context = contextvars.ContextVar(
73-
"connection_context"
74-
) # type: contextvars.ContextVar
72+
self._connection_context = ContextVar("connection_context") # type: ContextVar
7573

7674
# When `force_rollback=True` is used, we use a single global
7775
# connection, within a transaction that always rolls back.
@@ -120,7 +118,7 @@ async def disconnect(self) -> None:
120118
self._global_transaction = None
121119
self._global_connection = None
122120
else:
123-
self._connection_context = contextvars.ContextVar("connection_context")
121+
self._connection_context = ContextVar("connection_context")
124122

125123
await self._backend.disconnect()
126124
logger.info(
@@ -182,35 +180,21 @@ async def iterate(
182180
async for record in connection.iterate(query, values):
183181
yield record
184182

185-
def _new_connection(self) -> "Connection":
186-
connection = Connection(self._backend)
187-
self._connection_context.set(connection)
188-
return connection
189-
190183
def connection(self) -> "Connection":
191184
if self._global_connection is not None:
192185
return self._global_connection
193186

194187
try:
195188
return self._connection_context.get()
196189
except LookupError:
197-
return self._new_connection()
190+
connection = Connection(self._backend)
191+
self._connection_context.set(connection)
192+
return connection
198193

199194
def transaction(
200195
self, *, force_rollback: bool = False, **kwargs: typing.Any
201196
) -> "Transaction":
202-
try:
203-
connection = self._connection_context.get()
204-
is_root = not connection._transaction_stack
205-
if is_root:
206-
newcontext = contextvars.copy_context()
207-
get_conn = lambda: newcontext.run(self._new_connection)
208-
else:
209-
get_conn = self.connection
210-
except LookupError:
211-
get_conn = self.connection
212-
213-
return Transaction(get_conn, force_rollback=force_rollback, **kwargs)
197+
return Transaction(self.connection, force_rollback=force_rollback, **kwargs)
214198

215199
@contextlib.contextmanager
216200
def force_rollback(self) -> typing.Iterator[None]:

tests/test_databases.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,25 +1123,6 @@ async def test_column_names(database_url, select_query):
11231123
assert results[0]["completed"] == True
11241124

11251125

1126-
@pytest.mark.parametrize("database_url", DATABASE_URLS)
1127-
@mysql_versions
1128-
@async_adapter
1129-
async def test_parallel_transactions(database_url):
1130-
"""
1131-
Test parallel transaction execution.
1132-
"""
1133-
1134-
async def test_task(db):
1135-
async with db.transaction():
1136-
await db.fetch_one("SELECT 1")
1137-
1138-
async with Database(database_url) as database:
1139-
await database.fetch_one("SELECT 1")
1140-
1141-
tasks = [test_task(database) for i in range(4)]
1142-
await asyncio.gather(*tasks)
1143-
1144-
11451126
@pytest.mark.parametrize("database_url", DATABASE_URLS)
11461127
@async_adapter
11471128
async def test_posgres_interface(database_url):

0 commit comments

Comments
 (0)