Method Example of Automatic Reconnection of django Database

  • 2021-07-24 11:16:43
  • OfStack

Brief introduction

Automatically reconnect the database if the connection is lost due to Django database connection exceeding wait_timeout

https://github.com/zhanghaofe... (Download locally)

Installation


pip install django_db_reconnect

Note that only pymysql is supported. To make django use pymysql, you need to install the package first and add the following code to __init__. py in the directory where settings. py is located:


import pymysql
pymysql.install_as_MySQLdb()

Use

Add django_db_reconnect to settings. py's INSTALLED_APPS


INSTALLED_APPS = (
 #  Omit other configurations 
 'django_db_reconnect',
)

Other issues

Transactions or other autocommit=False non-automatic commit cases will not be automatically reconnected, otherwise writes before the connection is lost may be discarded without commit

Summarize


Related articles: