NoobEditor
NoobEditor

Reputation: 15871

work-around for (2006, 'MySQL server has gone away')?

This question is my exact issue

Django - OperationalError: (2006, 'MySQL server has gone away')

An aparent work-around to this otherwise unresolved problem is to increase the wait_timeout for the execution

Background

I have a celery task which runs at a specific time once a day. Initially it was working fine but from last week i have started getting :

Exception_ocoured_: (2013, 'Lost connection to MySQL server during query')

This celery task simply fetches some details from db, max of 4000 rows and mails to the end user.

Question :

Is there any way to increase this timeout only for the specific celery task which is facing this issue in django environment, as i don't want to disturb the native setup?

I am looking for a djangoish solution whose lifetime is only as long as this celery task executes.

For eg :

@task
def doSomething():
    try:
       set_timeout_for_mysql = 20000 # <== main agenda for this question
       # OR
       ping_resp = somehow_test_mysql_con()
       while(ping_resp == False):
          keep trying to connect or create new connection
       # do_operations 
    except Exception, e:
       # log exception

Spec :

In [18]: django.VERSION
Out[18]: (1, 7, 7, 'final', 0)

and

django-celery==3.0.21

PS : Any other workaround will do if someone has resolved this without disturbing the core setup!!!

Upvotes: 1

Views: 2415

Answers (2)

gzerone
gzerone

Reputation: 2227

from django.db import close_old_connections

...
close_old_connections()
...  # do some db jobs, it will reconnect db

Good Luck

Upvotes: 4

Bernd Buffen
Bernd Buffen

Reputation: 15057

You can set the wait_timeout for each Session (Connection)

set wait_timeout=10000;
SHOW VARIABLES LIKE 'wait_timeout';

but, are you sure that the error is from the wait_timeout. a other solution is the max_allowed_packet. You can increase it to see if this the Problem.

Upvotes: 0

Related Questions