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

前端 未结 4 1344
陌清茗
陌清茗 2021-02-19 04:56

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

4条回答
  •  别那么骄傲
    2021-02-19 05:29

    Since Django 1.6, you can use

    import django.db
    
    django.db.close_old_connections()
    

    This does basically the same thing as adamsmith's answer except that it handles multiple databases and also honors the CONN_MAX_AGE setting. Django calls close_old_connections() automatically before and after each request, so you normally don't have to worry about it unless you have some long-running code outside of the normal request/response cycle.

提交回复
热议问题