Пишу бота на discord.py. После обращения к боту выполняется запрос к бд и вытягивается информация. При первом обращении всё нормально, но если написать спустя пол часа после последнего сообщения, то выдаёт данную ошибку. Пробовал дописывать после запросов db.session.close(), но не помогает.
Полный код ошибки:
Ignoring exception in on_message
Traceback (most recent call last):
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\engine\base.py", line 1173, in _execute_context
context = constructor(dialect, self, conn, *args)
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\engine\default.py", line 768, in _init_compiled
self.cursor = self.create_cursor()
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\engine\default.py", line 1122, in create_cursor
return self._dbapi_connection.cursor()
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\pool\base.py", line 965, in cursor
return self.connection.cursor(*args, **kwargs)
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\mysql\connector\connection.py", line 809, in cursor
raise errors.OperationalError("MySQL Connection not available.")
mysql.connector.errors.OperationalError: MySQL Connection not available.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\discord\client.py", line 270, in _run_event
await coro(*args, **kwargs)
File "E:/python_projects/discord/app/bot.py", line 114, in on_message
chat = Chat.query.filter(Chat.user_id == message.author.id).first()
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\orm\query.py", line 3265, in first
ret = list(self[0:1])
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\orm\query.py", line 3043, in __getitem__
return list(res)
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\orm\query.py", line 3367, in __iter__
return self._execute_and_instances(context)
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\orm\query.py", line 3392, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\engine\base.py", line 982, in execute
return meth(self, multiparams, params)
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\sql\elements.py", line 287, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\engine\base.py", line 1101, in _execute_clauseelement
distilled_params,
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\engine\base.py", line 1176, in _execute_context
e, util.text_type(statement), parameters, None, None
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\engine\base.py", line 1476, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\util\compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\util\compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\engine\base.py", line 1173, in _execute_context
context = constructor(dialect, self, conn, *args)
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\engine\default.py", line 768, in _init_compiled
self.cursor = self.create_cursor()
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\engine\default.py", line 1122, in create_cursor
return self._dbapi_connection.cursor()
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\sqlalchemy\pool\base.py", line 965, in cursor
return self.connection.cursor(*args, **kwargs)
File "E:\python_projects\bubux_test_bot\env\lib\site-packages\mysql\connector\connection.py", line 809, in cursor
raise errors.OperationalError("MySQL Connection not available.")
sqlalchemy.exc.OperationalError: (mysql.connector.errors.OperationalError) MySQL Connection not available.
[SQL: SELECT chat.id AS chat_id_1, chat.user_id AS chat_user_id, chat.chat_id AS chat_chat_id
FROM chat
WHERE chat.user_id = %(user_id_1)s
LIMIT %(param_1)s]
[parameters: [immutabledict({})]]
(Background on this error at: http://sqlalche.me/e/e3q8)