I have a question about database connection: Is it possible to make Django retry on database connection failures?
According to the official documentation, Django will close the db connection if it’s no longer alive (https://docs.djangoproject.com/en/3.1/ref/databases/#persistent-connections). Is there a way (a library or configuration setting) to make it retry on connection errors instead of closing it directly?
We could wrap around the orm like this @retry(
stop=stop_after_attempt(3),
wait=wait_fixed(2),
retry=lambda retry_state: isinstance(retry_state.outcome.exception(), OperationalError)
def get_data():
return MyModel.objects.all()
it will try to connect to databse if it fails it will retry three times if still fails it will throw error.