Error sqlalchemy: sqlalchemy.exc.OperationalError: (mysql.connector.errors.OperationalError) MySQL Connection not available. How to solve?

Write bot discord.py. After contacting the bot performs a request to the database and extracted information. The first call everything is fine, but if you write after half an hour after the last message, it gives this error. I tried to write after the db queries.session.close () but nothing works.
Full error code:
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.the 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)
April 3rd 20 at 18:39
1 answer
April 3rd 20 at 18:41
Solution
It is enough to read documentation and write code correctly:
https://docs.sqlalchemy.org/en/13/dialects/mysql.h...

Connection Timeouts and Disconnects
MySQL, by default, automatically closes connections if they are idle opredelennoe time(default 8 hours). To work around this issue, use the parameter to create_engine.pool_recycle ,which specifies the time after which the connection is updated if it exists already so many seconds:
engine = create_engine('mysql+mysqldb://...', pool_recycle=3600)

To detect other types of breaks need to use a periodic ping.
In this case, may not be correct? I described the class to the table:
class Chat(db.Model):
 id = db.Column(db.Integer, primary_key=True)
 user_id = db.Column(db.String(150))
 chat_id = db.Column(db.String(150))

And then call:
chat = Chat.query.filter(Chat.user_id == message.author.id).first()
- hortense.Hickle commented on April 3rd 20 at 18:44
Well, the base itself throws for inactivity(for the symptoms described as suited) is a feature of MySQL.
I realized, "Chukchi not a reader" so even translated the paragraph on the link - so others can find it. - Margarett commented on April 3rd 20 at 18:47
@Margarett, most likely not prescribe.
File bot.py:
from flask import Flask
from SQLAlchemy import flask_sqlalchemy
from config import Configuration

app = Flask(__name__)
app.config.from_object(Configuration)
db = SQLAlchemy(app)

And file config.py:
from sqlalchemy import create_engine

class Configuration(object):
 SQLALCHEMY_TRACK_MODIFICATIONS = False
 SQLALCHEMY_DATABASE_URI = create_engine('mysql+mysqlconnector:setting', pool_recycle=3600)
 }

With these settings generates an error: AttributeError: 'Engine' object has no attribute 'drivername'.
Maybe what create_engine cannot be used in SQLALCHEMY_DATABASE_URI? - hortense.Hickle commented on April 3rd 20 at 18:50

Find more questions by tags MySQLPythonBotsSQLAlchemy