7

I have a MySQL gone away with Django under WSGI. I found entries for this problem on stackoverflow, but nothing with Django specifically. Google does not help, except for workarounds (like polling the website every once in a while, or increasing the database timeout). Nothing definitive. Technically, Django and/or MySQLdb (I'm using the latest 1.2.3c1) should attempt a reconnect if the server hanged the connection, but this does not happen. How can I solve this issue without workarounds ?

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
  • Are the Webserver and MySQL on the same machine? If not, the closer they are the less likeley it is that a network problem will cause the server to 'go away'. – lexu Apr 06 '10 at 05:19
  • select version(); show processlist; show variables like '%max%; Please paste output: – baklarz2048 Nov 10 '10 at 19:42
  • For anyone coming down this path in the future, here's the reasons MySQL could be returning this error: http://dev.mysql.com/doc/refman/5.0/en/gone-away.html – NotSimon Jun 10 '14 at 21:25
  • We can reset the connection with a single line before the problem line, see my answer in another question: https://stackoverflow.com/a/67917637/2544762 – Alfred Huang Jun 11 '21 at 06:54

3 Answers3

3

Django developers gave one short answer for all questions like this in https://code.djangoproject.com/ticket/21597#comment:29

  • Resolution set to wontfix

Actually this is the intended behavior after #15119. See that ticket for the rationale.

If you hit this problem and don't want to understand what's going on, don't reopen this ticket, just do this:

  • RECOMMENDED SOLUTION: close the connection with from django.db import connection; connection.close() when you know that your program is going to be idle for a long time.

  • CRAPPY SOLUTION: increase wait_timeout so it's longer than the maximum idle time of your program.

In this context, idle time is the time between two successive database queries.

Community
  • 1
  • 1
ta2-1
  • 411
  • 4
  • 6
  • For the first way we can also do the stuff before we do the action, see my answer in another question: https://stackoverflow.com/a/67917637/2544762 – Alfred Huang Jun 11 '21 at 06:54
3

show variables like 'wait_timeout';

this is the setting will throw back the "mysql gone away" error
set it to a very large value to prevent it "gone away"
or simple re-ping the mysql connection after certain period

ajreal
  • 46,720
  • 11
  • 89
  • 119
1
  • You could create middleware to ping() the MySQL connection (which will reconnect if it timed out) before processing the view

  • You could also add middleware to catch the exception, reconnect, and retry the view (I think I would prefer the above solution as simpler, but it should technically work and be performant assuming timeouts are rare. This also assumes a failed view has no side effects, which is a desirable property but can be difficult to do, especially if you write to a filesystem as well as a db in your view.)

Eloff
  • 20,828
  • 17
  • 83
  • 112