Consider closing DB connections explicitly in the default database health check #685
-
|
Hi! In one of my deployments, the health check endpoint is hit every That resulted in a gradual increase in open database connections caused only by health check traffic. To avoid that, I introduced a custom database check that always closes the connection in a finally block after running the query. connection = connections[self.alias]
try:
...
finally:
connection.close()From my side, it seems the default database check could be safer if it explicitly closed the connection after each run, especially for deployments where:
I am not sure whether this should be the default behavior for everyone. I ended up solving this with a custom check, but I thought it might be worth discussing whether the base implementation should handle this case more defensively. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
Hi @ZhdanovAM72, like this? django-health-check/health_check/checks.py Lines 136 to 137 in a922d34 Thanks for reaching out. We have been closing connections since v3 and continue to do so in v4. Even though the latter users async methods, where Django handles connection closing for new threads. Since you are experiencing connections from staying open, I'd recommend revisiting the documentation about persistent connections. If you happen to use PostgreSQL, I highly recommend setting up PGBouncer to pool connections. Please let me know if you have more questions about this topic. Cheers! |
Beta Was this translation helpful? Give feedback.
@ZhdanovAM72 turns out there was room for improvement here; see #701 Version 4.2.2 should resolve this issue.