Made closing in connection handlers more DRY.

This commit is contained in:
Nick Pope 2022-05-12 14:13:51 +01:00 committed by GitHub
parent 3a82b5f655
commit 20e65a34ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 10 deletions

View File

@ -60,8 +60,7 @@ cache = ConnectionProxy(caches, DEFAULT_CACHE_ALIAS)
def close_caches(**kwargs): def close_caches(**kwargs):
# Some caches need to do a cleanup at the end of a request cycle. If not # 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. # implemented in a particular backend cache.close() is a no-op.
for cache in caches.all(initialized_only=True): caches.close_all()
cache.close()
signals.request_finished.connect(close_caches) signals.request_finished.connect(close_caches)

View File

@ -190,14 +190,6 @@ class ConnectionHandler(BaseConnectionHandler):
backend = load_backend(db["ENGINE"]) backend = load_backend(db["ENGINE"])
return backend.DatabaseWrapper(db, alias) 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: class ConnectionRouter:
def __init__(self, routers=None): def __init__(self, routers=None):

View File

@ -79,3 +79,7 @@ class BaseConnectionHandler:
# If initialized_only is True, return only initialized connections. # If initialized_only is True, return only initialized connections.
if not initialized_only or hasattr(self._connections, alias) if not initialized_only or hasattr(self._connections, alias)
] ]
def close_all(self):
for conn in self.all(initialized_only=True):
conn.close()