Close database connection for long running django-admin command

551 views
Skip to first unread message

Charanpal Dhanjal

unread,
Aug 24, 2016, 1:16:36 PM8/24/16
to Django users
Hi all,

I am using GeoDjango and I have a django-admin command that does some route calculations for approximately 24 hours and then saves the results to a MySQL database. Unfortunately the database connection times out after 8 hours resulting in: django.db.utils.OperationalError: (2006, 'MySQL server has gone away'). I check in the mysql logs that this is indeed a timeout error. It seems to be the case that django is maintaining a persistent connection even when I set CONN_MAX_AGE to 60 seconds (although perhaps this parameter applies only to requests). Is there a solution to this issue without simply increasing the timeout in MySQL?

So far I have tried closing the connections manually using:

from django.db import connections
connections.close_all()

I also tried simply catching the exception and retrying the write to the database (it's stated that Django automatically reopens connections on new queries here: https://docs.djangoproject.com/en/1.10/ref/databases/#connection-management), but the same error occurs.

I am using Django 1.10 and Python 2.7.12 on Linux, if that helps.

Any help is greatly appreciated.

Thanks in advance,

Charanpal

18626...@163.com

unread,
Nov 1, 2017, 10:43:18 PM11/1/17
to Django users
Did you solve this problem?
Reply all
Reply to author
Forward
0 new messages