处理SQLAlchemy断开连接 [英] Handle SQLAlchemy disconnect
问题描述
我在Flask应用程序中使用SQLAlchemy,但在关闭与MySQL数据库的连接时遇到了一些问题.
I'm using SQLAlchemy for my Flask application and I have just ran into a few problems with closed connection to my MySQL DB.
我正在我的__init__.py
engine = create_engine('mysql://user:pass@localhost/db', pool_recycle=3600)
db = SQLAlchemy(app)
(我同时使用两种方法查询数据库)
(I am using both ways to query my DB)
我刚刚添加了pool_recycle设置,但是我想确保丢失的连接可以重新启动.
I have just added the pool_recycle setting but I would like to be sure a lost connection can be restarted.
阅读以下部分: http://docs.sqlalchemy.org/en/latest/core/pooling.html#disconnect-handling-pessimistic
我不太确定如何实际使用此代码,我不想每次在代码中使用连接时都要检查连接,而是希望在连接关闭时重新启动连接.是否可以添加一个常规事件来处理数据库断开连接?
I'm not really sure how to actually use this code, I don't want have to check the connection every time its being used in the code but rather have the connection restarted if it would close down. Is it possible to add a general event to handle DB disconnections?
推荐答案
最好将sqlalchemy与poolclass = NullPoll(不是持久连接)一起用于具有高流量的项目,因为存在大量并发连接
is better use sqlalchemy with poolclass=NullPoll(not persistent connections) on proyects with hight traffic since there are so much connections concurrents
出于我的经验,我始终将slqalchemy与该功能配合使用
for my expirience i always use slqalchemy with this feature
这篇关于处理SQLAlchemy断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!