diff options
author | Jon Dufresne <jon.dufresne@gmail.com> | 2020-02-03 19:07:00 -0800 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-02-06 15:29:38 +0100 |
commit | f48f671223a20b161ca819cf7d6298e43b8ba5fe (patch) | |
tree | bc24e2f0ae10a4ffd088141e2681b45173c08b53 /django/db/backends/postgresql/base.py | |
parent | 2d55cb5c4a9c736d04290c9f99ebcbe9e196ac97 (diff) | |
download | django-f48f671223a20b161ca819cf7d6298e43b8ba5fe.tar.gz |
Refs #31233 -- Changed DatabaseWrapper._nodb_connection to _nodb_cursor().
It is now a method instead of a property and returns a context manager
that yields a cursor on entry and closes the cursor and connection upon
exit.
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): |