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()