Python/金字塔 + SQLAlchemy > MySQL : How do I check connection before usage?

标签 python mysql sqlalchemy

我被告知数据库超时(连接、交互、等待)设置为 30 秒。该数据库上有一个网站,人们往往会闲置超过 30 秒。

使用sqlalchemy,我需要在继续查询之前检查mysql连接是否仍然有效,否则站点会出错。

我见过this question ,但我没有池化连接,也没有可靠性问题。

最佳答案

SQLAlchemy 的引擎都是作为池实现的,即使它们实际上并不池化。

阅读先人所写的这篇文章:SQLAlchemy – MYSQL has gone away 。简而言之:

The first thing to make sure of is that you set the ‘pool_recycle option’ when using SQLAlchemy ‘create_engine’ function (more here). The value of pool_recycle should be less than your MYSQLs wait_timeout value (both are in seconds).

The next and altogether knottier problem is to make sure you correctly manage your session life cycle. It’s well worth carefully reading this documentation. Essentially, you have to make sure that your session is correctly opened when your data request starts and closed when it’s done.

关于Python/金字塔 + SQLAlchemy > MySQL : How do I check connection before usage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29885925/

相关文章:

python - SQLAlchemy 声明式。指定要选择的列

python - 列中的最后一条记录,SQLALCHEMY

Python 打印函数在解释器中获得意外输出?

python - 尝试创建 GAN : InvalidArgumentError: Matrix size-incompatible

python - Flask-Admin 具有多对多关系中的附加字段

PHP - SQL - DELETE 查询不起作用

mysql - 无法调试 Windows 窗体程序

python - 如何在 matplotlib 中绘制半无限线(射线)?

mysql - 如何在 Bash 中保持 MySQL 连接打开

python - SQLAlchemy 自动提交?