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.

Installation

  1. Install the package from PyPi:

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

     INSTALLED_APPS = [
         'dbconnectionretrier',
         ...
     ]
    

Manual usage

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

Globally

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
patch.rollback()

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.

Source Distribution

django-db-connection-retrier-1.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file django-db-connection-retrier-1.0.tar.gz.

File metadata

  • Download URL: django-db-connection-retrier-1.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.8.2

File hashes

Hashes for django-db-connection-retrier-1.0.tar.gz
Algorithm Hash digest
SHA256 03728c748644fdc28e015c8df3f9e7038aca96522325bb3be4fd89f6a3787146
MD5 90d413b95691b0e24a8bce3e8dd81075
BLAKE2b-256 06c4f1dfc34a45ad8f03a9d5a5b80d6ca379efe2a5a6757218564aebecfcdf51

See more details on using hashes here.

File details

Details for the file django_db_connection_retrier-1.0-py3-none-any.whl.

File metadata

  • Download URL: django_db_connection_retrier-1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.8.2

File hashes

Hashes for django_db_connection_retrier-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b88c26a66a2a97ee8f5ea657a0437c76fd9bbf6654c7832aeda6421e6c862cfc
MD5 98e4057e266f519d07e1c4c72922f9e9
BLAKE2b-256 0d356dd9edf163512611b30c2792f80cd5e1930aec4ca6ac2e213317dd1e7fe2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page