Python/Pyramid+SQLAlchemy>MySQL:如何在使用前检查连接?

2024-04-19 07:04:19 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我见过this question,但我没有共用连接,也没有可靠性问题。在


Tags: 数据库sqlalchemy站点mysqlthis空闲question可靠性
1条回答
网友
1楼 · 发布于 2024-04-19 07:04:19

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.

相关问题 更多 >