英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论