Relative Content

Tag Archive for pythonmysqlflasksqlalchemy

SQLAlchemy – MySQL database connection timeouts

I am having a webapp with python as backend. In this webapp, I need a connection to my MySQL database, which I establish by using SQLAlchemy. The problem is that whenever I start my application on the server, the next day I receive this exception, when calling a function that tries to access the database:
sqlalchemy.exc.OperationalError: (MySQLdb.OperationalError) (1161, ”)

Even if SQLALCHEMY_POOL_RECYCLE is set, flask will not automatically recycle mysql connections

Recently, my flask program encountered the error of Lost connection to MySQL server during query. I searched some documents and discussions. It seems that MySQL timed out and disconnected the connection, but flask did not recycle these expired connections.
I am currently using Flask_SQLAlchemy==2.4.4. From the documentation of flask 2.x, I learned that setting SQLALCHEMY_POOL_RECYCLE can adjust the time for flask to recycle expired connections. I tried setting it but it did not solve my problem.
This is my demo: