Skip to content

Commit 20e65a3

Browse files
authored
Made closing in connection handlers more DRY.
1 parent 3a82b5f commit 20e65a3

File tree

3 files changed

+5
-10
lines changed

3 files changed

+5
-10
lines changed

django/core/cache/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ def create_connection(self, alias):
6060
def close_caches(**kwargs):
6161
# Some caches need to do a cleanup at the end of a request cycle. If not
6262
# implemented in a particular backend cache.close() is a no-op.
63-
for cache in caches.all(initialized_only=True):
64-
cache.close()
63+
caches.close_all()
6564

6665

6766
signals.request_finished.connect(close_caches)

django/db/utils.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,6 @@ def create_connection(self, alias):
190190
backend = load_backend(db["ENGINE"])
191191
return backend.DatabaseWrapper(db, alias)
192192

193-
def close_all(self):
194-
for alias in self:
195-
try:
196-
connection = getattr(self._connections, alias)
197-
except AttributeError:
198-
continue
199-
connection.close()
200-
201193

202194
class ConnectionRouter:
203195
def __init__(self, routers=None):

django/utils/connection.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,7 @@ def all(self, initialized_only=False):
7979
# If initialized_only is True, return only initialized connections.
8080
if not initialized_only or hasattr(self._connections, alias)
8181
]
82+
83+
def close_all(self):
84+
for conn in self.all(initialized_only=True):
85+
conn.close()

0 commit comments

Comments
 (0)