├── .gitignore ├── README.md ├── django_db_reconnect └── __init__.py └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | .svn/ 2 | *.swp 3 | *.swo 4 | .idea/ 5 | *~ 6 | *.pyc 7 | *.log 8 | *.log.* 9 | *.bak 10 | *.bat 11 | /log 12 | .settings 13 | .project 14 | .pydevproject 15 | local_settings.py 16 | .DS_Store 17 | *.log.* 18 | .env* 19 | *___jb_old___ 20 | *___jb_bak___ 21 | dist/ 22 | *.egg-info/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 简介 2 | 3 | Django数据库连接超过wait_timeout导致连接丢失时自动重新连接数据库 4 | 5 | # 安装 6 | 7 | ```sh 8 | pip install django_db_reconnect 9 | ``` 10 | > 注意仅支持`pymysql`,使django使用`pymysql`需要先安装包并在settings.py所在目录的__init__.py增加如下代码: 11 | > ``` 12 | > import pymysql 13 | > pymysql.install_as_MySQLdb() 14 | > ``` 15 | 16 | # 使用 17 | 18 | 添加`django_db_reconnect`到settings.py的INSTALLED_APPS 19 | ```python 20 | INSTALLED_APPS = ( 21 | # 省略其他配置 22 | 'django_db_reconnect', 23 | ) 24 | ``` 25 | 26 | # 其他问题 27 | 1. 事务或者其他autocommit=False非自动提交情况下将不会自动重连,否则可能导致连接丢失前的写入没有commit被丢弃 -------------------------------------------------------------------------------- /django_db_reconnect/__init__.py: -------------------------------------------------------------------------------- 1 | # -* encoding: utf-8 *- 2 | import logging 3 | 4 | from django.db.backends.base import base as django_db_base 5 | 6 | try: 7 | import pymysql 8 | except ImportError: 9 | pymysql = None 10 | 11 | log = logging.getLogger('django-db-reconnect') 12 | 13 | 14 | def monkey_patch() -> None: 15 | _old_ensure_connection = django_db_base.BaseDatabaseWrapper.ensure_connection 16 | 17 | def ensure_connection_with_retries(self: django_db_base.BaseDatabaseWrapper) -> None: 18 | if self.connection: 19 | if self.autocommit: 20 | if pymysql and isinstance(self.connection, pymysql.connections.Connection): 21 | self.connection.ping(reconnect=True) 22 | 23 | _old_ensure_connection(self) 24 | 25 | log.debug('monkey path django.db.backends.base.BaseDatabaseWrapper.ensure_connection') 26 | django_db_base.BaseDatabaseWrapper.ensure_connection = ensure_connection_with_retries 27 | 28 | 29 | monkey_patch() 30 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from __future__ import with_statement, print_function 3 | 4 | from setuptools import setup, find_packages 5 | 6 | setup( 7 | name='django_db_reconnect', 8 | version='0.0.1', 9 | author='zhanghaofei', 10 | author_email='1042697116@qq.com', 11 | packages=['django_db_reconnect'], 12 | description='Django数据库自动重连', 13 | url='', 14 | include_package_data=True, 15 | classifiers=[ 16 | 'Development Status :: 5 - Production/Stable', 17 | 'License :: OSI Approved :: MIT License', 18 | 'Operating System :: MacOS', 19 | 'Operating System :: POSIX', 20 | 'Operating System :: POSIX :: Linux', 21 | 'Programming Language :: Python', 22 | 'Programming Language :: Python :: 2.7', 23 | 'Programming Language :: Python :: 3.4', 24 | 'Programming Language :: Python :: 3.5', 25 | 'Programming Language :: Python :: 3.6', 26 | 'Programming Language :: Python :: Implementation :: CPython', 27 | 'Programming Language :: Python :: Implementation :: PyPy', 28 | 'Intended Audience :: Developers', 29 | 'Topic :: Software Development :: Libraries', 30 | 'Topic :: Software Development :: Libraries :: Python Modules', 31 | 'Topic :: Utilities', 32 | ] 33 | ) 34 | --------------------------------------------------------------------------------