# Automatic retry of failed database operations

**URL:** <https://forum.djangoproject.com/t/automatic-retry-of-failed-database-operations/27530>\
**Category:** Using the ORM\
**Created:** [January 29, 2024, 3:49pm UTC](https://forum.djangoproject.com/t/automatic-retry-of-failed-database-operations/27530 "2024-01-29T15:49:04Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![koendewit](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/koendewit/32/18680_2.png) [@koendewit](https://forum.djangoproject.com/u/koendewit)\
**Post date:** [January 29, 2024, 3:49pm UTC](https://forum.djangoproject.com/t/automatic-retry-of-failed-database-operations/27530/1 "2024-01-29T15:49:04Z")

</div>

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 [Manage database connections &nbsp;|&nbsp; Cloud SQL for PostgreSQL &nbsp;|&nbsp; Google Cloud](https://cloud.google.com/sql/docs/postgres/manage-connections), section “Exponential backoff”) How can I implement this 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. How can I retry the database operation so that the view is finally rendered successfully?
