Skip to content

Instantly share code, notes, and snippets.

@jmg
Last active May 6, 2016 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmg/1512da0396de8f7fbbe82734d954136a to your computer and use it in GitHub Desktop.
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.
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