Skip to main content

Automatically ty re-establishing the Django database connection when it gets lost.

Project description

Django Database Connection Retrier

License PyPi CircleCI

Automatically try to re-establish Django database connections when they fail due to DNS errors.

When the Django app (dbconnectionretrier) loads, a hook is installed in Django's BaseDatabaseWrapper.ensure_connection that catches django.db.OperationalError. If the error raised is a DNS error, the connction attempt is retried synchronously three times.

Tested on Django 2.0-2.3 and Python 3.7 and 3.8. Might work with other versions of Django and Python.


  1. Install the package from PyPi:

     $ pip install django-db-connection-retrier
  2. Add dbconnectionretrier to your INSTALLED_APPS:


Manual usage

Adding dbconnectionretrier to INSTALLED_APPS enables automatic connection retrying. Want more granular control over patching?


from dbconnectionretrier.patch import patch_ensure_connection

# after this line executes, connection retrying is enabled
patch = patch_ensure_connection()

# after this line, the connection retrying is disabled

Context manager

from dbconnectionretrier.patch import patch_ensure_connection_contextual

with patch_ensure_connection_contextual():
    # all code inside this block benefits from connection retrying

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-db-connection-retrier, version 1.0
Filename, size File type Python version Upload date Hashes
Filename, size django_db_connection_retrier-1.0-py3-none-any.whl (5.1 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size django-db-connection-retrier-1.0.tar.gz (5.0 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page