diff --git a/README.md b/README.md index 86f2c89..c9b4247 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,10 @@ +# 基于下面库进行优化,主要适配Django==5.0.3以后的版本。修复原子事务报错的问题 + +安装:`pip install mysql_server_has_gone` +因为代码里用到了MySQLdb库,所以需要安装mysqlclient。如果你用到了其他库导致报错,可以github上滴滴我,我会及时修复的。 + +以下是原始文档: + # MySQL-server-has-gone-away This repository solves issue where database connection inside of django overlives MySQL database connection timeout specified in `/etc/my.cnf` `wait_timeout = xxxx`. [See this issue](https://stackoverflow.com/questions/26958592/django-after-upgrade-mysql-server-has-gone-away) diff --git a/mysql_server_has_gone_away/base.py b/mysql_server_has_gone_away/base.py index 66b3a46..1e1cb2c 100644 --- a/mysql_server_has_gone_away/base.py +++ b/mysql_server_has_gone_away/base.py @@ -4,21 +4,27 @@ import logging -from django.db import OperationalError, InterfaceError, IntegrityError from django.db.backends.mysql import base +from django.db.utils import IntegrityError, InterfaceError, OperationalError +from MySQLdb import OperationalError as MySQLOperationalError +from retry import retry + +logger = logging.getLogger("mysql_server_has_gone_away") -logger = logging.getLogger('mysql_server_has_gone_away') def check_mysql_gone_away(db_wrapper): def decorate(f): + @retry(tries=5) def wrapper(self, query, args=None): try: return f(self, query, args) - except (OperationalError, InterfaceError) as e: - logger.warn("MySQL server has gone away. Rerunning query: %s", query) + except (OperationalError, InterfaceError, MySQLOperationalError) as e: + logger.error(f"MySQL server has gone away. Rerunning query: {query}; Error: {e}") if ( - 'MySQL server has gone away' in str(e) or - 'Lost connection to MySQL server during query' in str(e) + "MySQL server has gone away" in str(e) + or "Server has gone away" in str(e) + or "Lost connection to MySQL server during query" in str(e) + or "The client was disconnected by the server because of inactivity" in str(e) ): db_wrapper.connection.close() db_wrapper.connect() @@ -29,6 +35,7 @@ def wrapper(self, query, args=None): if e.args[0] in self.codes_for_integrityerror: raise IntegrityError(*tuple(e.args)) raise + return wrapper return decorate @@ -50,3 +57,9 @@ def executemany(self, query, args): cursor = self.connection.cursor() return CursorWrapper(cursor) + + def _set_autocommit(self, autocommit): + try: + super()._set_autocommit(autocommit) + except (OperationalError, MySQLOperationalError) as e: + logger.error(f"DatabaseWrapper Error: {e}") diff --git a/setup.py b/setup.py index e57b9d2..38b4b74 100644 --- a/setup.py +++ b/setup.py @@ -1,16 +1,16 @@ -from setuptools import setup, find_packages +from setuptools import find_packages, setup with open("README.md", "r") as fh: long_description = fh.read() setup( - name="mysql_server_has_gone_away", - version="1.0.0", + name="mysql_server_has_gone", + version="2.0.5", description="Django myslq backend that fixes issue with long living connection", - author='Andrew Koidan', - author_email='deathangel908@gmail.com', + author="maiyajj", + author_email="1045373828@qq.com", license="MIT", - url='https://github.com/akoidan/MySQL-server-has-gone-away', + url="https://github.com/maiyajj/MySQL-server-has-gone-away", long_description=long_description, packages=find_packages(), long_description_content_type="text/markdown",