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

fix 数据库连接超时断开导致的'Lost connection to MySQL server during query'报错 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
19 changes: 18 additions & 1 deletion core/util/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@

from hoshino import logger

class RetryOperationalError(object):
def execute_sql(self, sql, params=None, commit=True):
try:
cursor = super(RetryOperationalError, self).execute_sql(
sql, params, commit)
except peewee.OperationalError:
if not self.is_closed():
self.close()
with peewee.__exception_wrapper__:
cursor = self.cursor()
cursor.execute(sql, params or ())
if commit and not self.in_transaction():
self.commit()
return cursor
class RetryMySQLDatabase(RetryOperationalError, peewee.MySQLDatabase):
pass

try:
from hoshino.config.__bot__ import (MySQL_host, MySQL_password, MySQL_port,
MySQL_username)
Expand All @@ -25,7 +42,7 @@

def database(class_name):
return \
peewee.MySQLDatabase(
RetryMySQLDatabase(
host=MySQL_host,
port=MySQL_port,
user=MySQL_username,
Expand Down