Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support new error msg #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
support new error msg
  • Loading branch information
程浩 committed Apr 24, 2024
commit bba81bf36ee8db28bfedcb8bf289e35fcf5afe94
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
26 changes: 19 additions & 7 deletions mysql_server_has_gone_away/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +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
'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()
Expand All @@ -30,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
Expand All @@ -51,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}")
12 changes: 6 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down