1

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!!!

Community
  • 1
  • 1
NoobEditor
  • 15,563
  • 19
  • 81
  • 112

2 Answers2

4
from django.db import close_old_connections

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

Good Luck

gzerone
  • 2,179
  • 21
  • 24
0

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.

NoobEditor
  • 15,563
  • 19
  • 81
  • 112
Bernd Buffen
  • 14,525
  • 2
  • 24
  • 39
  • `max_allowed_packet` and this `set wait_timeout`,not sure if i can i directly mention this in my celery `def` or does it need a separate implementation? – NoobEditor Sep 07 '15 at 11:29