Peewee在MySQL连接上的超时

huangapple go评论87阅读模式
英文:

Peewee timeout on mysql connection

问题

我有一个使用Peewee作为ORM的Flask应用程序。
一切都好,但我想处理数据库宕机的情况。

是否可以在连接上设置超时?如何处理Peewee中数据库宕机的情况?

我尝试使用connect来处理异常,但没有结果。

英文:

I have a flask application with Peewee as ORM.
All good but i want to manage the case where the database is down.

Is it possible to place a timeout on connection ? how do you manage the fact that the database is down with peewee ?

I tried to manage an exeption with connect but without result.

答案1

得分: 1

如果数据库宕机,您的应用程序能够可靠地运行吗?最好是注册一个错误处理程序 -- 这是一个接受异常的回调函数 -- 并根据错误的类型向用户显示数据库宕机的消息。

Flask 注册错误处理程序文档:https://flask.palletsprojects.com/en/2.2.x/errorhandling/#registering

您可以检查异常是否对应于数据库的连接错误,并在回调函数中采取适当的措施,比如渲染一个特殊页面。

英文:

If the database is down can your application reliably function? It might be best to register an error handler -- this is a callback that accepts the exception -- and, depending on the error you might display to the user a message that the database is down.

Flask register error handler doc: https://flask.palletsprojects.com/en/2.2.x/errorhandling/#registering

You can check that the exception corresponds to a connection error to the database and take proper action in the callback, such as rendering a special page.

huangapple
  • 本文由 发表于 2023年3月31日 21:03:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定