diff --git a/databasez/core/database.py b/databasez/core/database.py index 58c9d91..3b88b6d 100644 --- a/databasez/core/database.py +++ b/databasez/core/database.py @@ -343,11 +343,7 @@ async def connect(self) -> bool: ) # prevent side effects of connect_hook database._call_hooks = False - # only enter when full_isolation is active - if self._global_connection._full_isolation: - database._global_connection = await self._global_connection.__aenter__() - else: - database._global_connection = self._global_connection + database._global_connection = self._global_connection self._databases_map[loop] = database # forward call return await self._databases_map[loop].connect() @@ -408,8 +404,8 @@ async def disconnect( try: assert self._global_connection is not None - await self._global_connection.__aexit__() if self._remove_global_connection: + await self._global_connection.__aexit__() self._global_connection = None self._connection = None finally: diff --git a/databasez/core/transaction.py b/databasez/core/transaction.py index 77e7500..d98296d 100644 --- a/databasez/core/transaction.py +++ b/databasez/core/transaction.py @@ -117,11 +117,11 @@ async def wrapper(*args: typing.Any, **kwargs: typing.Any) -> typing.Any: @multiloop_protector(False) async def _start( self, - connection: Connection, timeout: typing.Optional[ float ] = None, # stub for type checker, multiloop_protector handles timeout ) -> None: + connection = self.connection assert connection._loop async with connection._transaction_lock: @@ -145,16 +145,22 @@ async def start( cleanup_on_error: bool = True, ) -> Transaction: connection = self.connection + # WARNING: we are maybe in the wrong context and get an AsyncDatabaseHelper, so + # - don't pass down the connection + # - assume this is not a connection_transaction # count up connection and init multithreading-safe the isolation thread # benefit 2: setup works with transaction_lock - if connection.connection_transaction is not self: + if getattr(connection, "connection_transaction", None) is not self: await connection.__aenter__() # we have a loop now in case of full_isolation try: - await self._start(connection, timeout=timeout) + await self._start(timeout=timeout) except BaseException as exc: # normal start call - if cleanup_on_error and connection.connection_transaction is not self: + if ( + cleanup_on_error + and getattr(connection, "connection_transaction", None) is not self + ): await connection.__aexit__() raise exc return self