Bottom line first: How do you refresh the MySQL connection in django?
Following a MySQL server has gone away
error I found that MySQL docum
The main reason that leads to this exception is mostly due to client ideal longer than wait_timeout
on mysql server.
In order to prevent that kind of error, django
supports an option named CONN_MAX_AGE
which allow django to recreate new connection if old connections are ideal too long.
So you should make sure that CONN_MAX_AGE
value is smaller than wait_timout
value.
One important thing is that, django
with wsgi
handles checking CONN_MAX_AGE
every requests by calling close_old_connections
. So you mainly don't need to care about that. However if you are using django
in standard alone application, there is no trigger to run that function. So you have to call it manually. So let call close_old_connections
in your code base.
Note: close_old_connections
will keep old connections if they're not expired yet. Your connections are still reused in case of high frequency query.