summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Pope <nick@nickpope.me.uk>2022-05-12 14:13:51 +0100
committerGitHub <noreply@github.com>2022-05-12 15:13:51 +0200
commit20e65a34aea0ace077033c84854dcf225e248f8c (patch)
tree134d285e207b161684e313ee21d5b3d346478ba7
parent3a82b5f655446f0ca89e3b6a92b100aa458f348f (diff)
Made closing in connection handlers more DRY.
-rw-r--r--django/core/cache/__init__.py3
-rw-r--r--django/db/utils.py8
-rw-r--r--django/utils/connection.py4
3 files changed, 5 insertions, 10 deletions
diff --git a/django/core/cache/__init__.py b/django/core/cache/__init__.py
index 613d3632ed..eb7fa5b2e9 100644
--- a/django/core/cache/__init__.py
+++ b/django/core/cache/__init__.py
@@ -60,8 +60,7 @@ cache = ConnectionProxy(caches, DEFAULT_CACHE_ALIAS)
def close_caches(**kwargs):
# Some caches need to do a cleanup at the end of a request cycle. If not
# implemented in a particular backend cache.close() is a no-op.
- for cache in caches.all(initialized_only=True):
- cache.close()
+ caches.close_all()
signals.request_finished.connect(close_caches)
diff --git a/django/db/utils.py b/django/db/utils.py
index 485d908fb9..3ad83b52ff 100644
--- a/django/db/utils.py
+++ b/django/db/utils.py
@@ -190,14 +190,6 @@ class ConnectionHandler(BaseConnectionHandler):
backend = load_backend(db["ENGINE"])
return backend.DatabaseWrapper(db, alias)
- def close_all(self):
- for alias in self:
- try:
- connection = getattr(self._connections, alias)
- except AttributeError:
- continue
- connection.close()
-
class ConnectionRouter:
def __init__(self, routers=None):
diff --git a/django/utils/connection.py b/django/utils/connection.py
index 5a82769cfc..a278598f25 100644
--- a/django/utils/connection.py
+++ b/django/utils/connection.py
@@ -79,3 +79,7 @@ class BaseConnectionHandler:
# If initialized_only is True, return only initialized connections.
if not initialized_only or hasattr(self._connections, alias)
]
+
+ def close_all(self):
+ for conn in self.all(initialized_only=True):
+ conn.close()