Database operations can fail because of temporary network errors or because the database is temporarily unavailable. Is there a way to automatically retry the operation in these cases ?
The documentation of Google's managed database offering states that connection pooling systems should reconnect to the database if a connection is in a bad state, and applications should retry a database transaction if an error app in the middle of a transaction. (See https://cloud.google.com/sql/docs/postgres/manage-connections, section "Exponential backoff")
Connection management with Django is clear to me, but how can I implement the second part of Google's advise using Django?
I'm currently having problems with views that successfully perform some database operations but randomly encounter a connection error during a subsequent database operation.
OperationalError at <url>
consuming input failed: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
/.../django/forms/widgets.py, line 657, in optgroups
657. for index, (option_value, option_label) in enumerate(self.choices):
/.../django/forms/models.py, line 1403, in __iter__
1403. for obj in queryset:
/.../django/db/backends/utils.py, line 80, in _execute_with_wrappers
80. return executor(sql, params, many, context)
/.../django/db/backends/utils.py, line 84, in _execute
84. with self.db.wrap_database_errors:
/.../django/db/utils.py, line 91, in __exit__
91. raise dj_exc_value.with_traceback(traceback) from exc_value
/.../django/db/backends/utils.py, line 89, in _execute
89. return self.cursor.execute(sql, params)
/.../psycopg/server_cursor.py, line 294, in execute
294. raise ex.with_traceback(None)
I'm not looking to solve this error, that would be impossible since it only happens every now and then (~0.001% of the queries) and it just means that the database is unavailable for a short time. I want to retry the database operation so that the view is finally rendered successfully.