From 2749b7de52fa15252afdb3f1ff6be9d29e74fbd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Hovm=C3=B6ller?= Date: Thu, 26 Sep 2024 08:54:04 +0200 Subject: [PATCH] Lock/unlock of db breaks if pytest is executed twice in the same process --- pytest_django/plugin.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pytest_django/plugin.py b/pytest_django/plugin.py index 62fa1791..44d98d84 100644 --- a/pytest_django/plugin.py +++ b/pytest_django/plugin.py @@ -800,8 +800,9 @@ def _dj_db_wrapper(self) -> django.db.backends.base.base.BaseDatabaseWrapper: # The first time the _dj_db_wrapper is accessed, we will save a # reference to the real implementation. - if self._real_ensure_connection is None: - self._real_ensure_connection = BaseDatabaseWrapper.ensure_connection + if not hasattr(BaseDatabaseWrapper, '_real_ensure_connection'): + BaseDatabaseWrapper._real_ensure_connection = BaseDatabaseWrapper.ensure_connection + self._real_ensure_connection = BaseDatabaseWrapper._real_ensure_connection return BaseDatabaseWrapper