Skip to content
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