Skip to content

Commit

Permalink
fix: close readiness conneciton (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
balavec authored Nov 20, 2024
1 parent 78bf22d commit eee9948
Showing 1 changed file with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,24 @@ def readiness(self, request):
# Connect to each database and do a generic standard SQL query
# that doesn't write any data and doesn't depend on any tables
# being present.
try:
from django.db import connections
from django.db import connections

for name in connections:
cursor = connections[name].cursor()
for name in connections:
cursor = None
connection = connections[name]
try:
cursor = connection.cursor()
cursor.execute("SELECT 1;")
row = cursor.fetchone()
if row is None:
return HttpResponseServerError("db: invalid response")
except Exception as e:
logger.exception(e)
return HttpResponseServerError("db: cannot connect to database.")
except Exception as e:
logger.exception(e)
return HttpResponseServerError("db: cannot connect to database.")
finally:
if cursor:
cursor.close()
connection.close()

# Call get_stats() to connect to each memcached instance and get its stats.
# This can effectively check if each is online.
Expand Down

0 comments on commit eee9948

Please sign in to comment.