Automatically ty re-establishing the Django database connection when it gets lost.
Project description
Django Database Connection Retrier
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
-
Install the package from PyPi:
$ pip install django-db-connection-retrier
-
Add
dbconnectionretrier
to yourINSTALLED_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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for django-db-connection-retrier-1.0b1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e0281970b9338eb30210a8a7dff004a12b80e5bba826870e9fee9f092bd7a9d9 |
|
MD5 | b0fcb78d23237efd31bcfc7794c024ab |
|
BLAKE2b-256 | 6e7657a89c1b6ff0b4fd24ec3172505b8fd0ce0911571c1159ee21b56ee746a9 |