Wednesday, November 1, 2017

Re: Close database connection for long running django-admin command

Did you solve this problem?

On Thursday, August 25, 2016 at 1:16:36 AM UTC+8, Charanpal Dhanjal wrote:
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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/18d2e8cf-64c6-4fd1-bcb0-4307b4b9cc10%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment