gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:30:03 29 4
gpt4 key购买 nike

我被告知数据库超时(连接、交互、等待)设置为 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com