Skip to content

Commit 79dd03e

Browse files
committed
Ref #701 -- Use temporary connection in DB check
Since the Database check is currently implemented syncronously, Django doesn't have a async ORM yet, we need to always close the connection. Django's DB connections are thread local and a new connection will be created for each run. Persisted connections will not be cleaned up. We can either call close or simply use a temporary connection.
1 parent bdc061a commit 79dd03e

File tree

1 file changed

+1
-4
lines changed

1 file changed

+1
-4
lines changed

health_check/checks.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,11 @@ def run(self):
118118
connection = connections[self.alias]
119119
except ConnectionDoesNotExist as e:
120120
raise ServiceUnavailable("Database alias does not exist") from e
121-
result = None
122121
try:
123122
compiler = connection.ops.compiler("SQLCompiler")(
124123
_SelectOne(), connection, None
125124
)
126-
with connection.cursor() as cursor:
125+
with connection.temporary_connection() as cursor:
127126
cursor.execute(*compiler.compile(_SelectOne()))
128127
result = cursor.fetchone()
129128
except db.Error as e:
@@ -133,8 +132,6 @@ def run(self):
133132
raise ServiceUnavailable(
134133
"Health Check query did not return the expected result."
135134
)
136-
finally:
137-
connection.close_if_unusable_or_obsolete()
138135

139136

140137
@dataclasses.dataclass

0 commit comments

Comments
 (0)