Last active
May 6, 2016 03:55
-
-
Save jmg/1512da0396de8f7fbbe82734d954136a to your computer and use it in GitHub Desktop.
Closes the db connection when it's gone because of a long period of inactivity (solves this issue https://code.djangoproject.com/ticket/21597). It forces django to use a new fresh connection.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import connection | |
from functools import wraps | |
def check_db_connection(f): | |
@wraps(f) | |
def inner(*args, **kargs): | |
try: | |
connection.connection.ping() | |
except: | |
connection.close() | |
return f(*args, **kargs) | |
return inner | |
#usage example: | |
# @app.task | |
# @check_db_connection | |
# def save_contacts(data): | |
# (...) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment