diff options
Diffstat (limited to 'django/db/backends/postgresql/base.py')
-rw-r--r-- | django/db/backends/postgresql/base.py | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py index 12b969142e..0f45ca93e1 100644 --- a/django/db/backends/postgresql/base.py +++ b/django/db/backends/postgresql/base.py @@ -7,6 +7,7 @@ Requires psycopg 2: https://www.psycopg.org/ import asyncio import threading import warnings +from contextlib import contextmanager from django.conf import settings from django.core.exceptions import ImproperlyConfigured @@ -288,11 +289,11 @@ class DatabaseWrapper(BaseDatabaseWrapper): else: return True - @property - def _nodb_connection(self): - nodb_connection = super()._nodb_connection + @contextmanager + def _nodb_cursor(self): try: - nodb_connection.ensure_connection() + with super()._nodb_cursor() as cursor: + yield cursor except (Database.DatabaseError, WrappedDatabaseError): warnings.warn( "Normally Django will use a connection to the 'postgres' database " @@ -304,11 +305,15 @@ class DatabaseWrapper(BaseDatabaseWrapper): ) for connection in connections.all(): if connection.vendor == 'postgresql' and connection.settings_dict['NAME'] != 'postgres': - return self.__class__( + conn = self.__class__( {**self.settings_dict, 'NAME': connection.settings_dict['NAME']}, alias=self.alias, ) - return nodb_connection + try: + with conn.cursor() as cursor: + yield cursor + finally: + conn.close() @cached_property def pg_version(self): |