doubi1931 2012-03-05 16:52
浏览 42

如果缺少数据库,则设计模式可以正常失败

I have an app under development which uses a database for a subset of its functionality. During development we have a higher than normal expectation that the database won't be available. Once launched, we would still like the main body of the application to work even if there is a database outage.

All of my database-facing classes extend a base class which grants them database access, however in the concrete classes there are some methods which perform selects directly (meaning not through the base classes methods).

Short of wrapping every database call in if statements, how can I gracefully degrade functionality when the database is not available?

EDIT

Failing gracefully for this app would be displaying a "service unavailable" message and stopping classes from attempting to access the database (which would throw errors).

  • 写回答

3条回答 默认 最新

  • dongwen5870 2012-03-05 17:22
    关注

    IMHO this falls under the wider scope of general error and exception handling.

    Some intro ...

    The database is essentially a global variable input in your code. If it's unavailable this is the very definition of an unexpected, "exceptional" condition; it should result in an exception. Of course, you explicitly asked how to handle the situation "short of wrapping every database call in if statements." Wrapping each and every DB operation in a try/catch block is basically the same as wrapping each operation in if statements.

    Further, the mysql_connect function will raise a PHP error in addition to returning FALSE if it can't connect. You should really be using PDO instead of the mysql dinosaur, but that's another topic entirely. Hopefully you aren't using the error suppression operator @ or not reporting E_WARNING errors to avoid this.

    So how to handle it ...

    Personally, I would have nothing to do with error handling in PHP. You should define a custom error handler function using set_error_handlerdocs that throws an exception on any PHP error and handle exceptions instead of errors.

    When you set up your error handling strategy in this manner it's then simple to wrap any functionality that could cause problems or unexpectedly throw in a try block and handle the unexpected events in your catch blocks.

    Since you stated that, "All of [your] database-facing classes extend a base class which grants them database access," I would suggest adding a simple boolean property to the base class to act as a flag for whether or not the database connection has failed. Then, just check the value of the flag before each attempt to query the database. If you're correctly passing around the database instance in your classes (and not breaking references), they'll all have access to the same property when they need it.

    If the database connection failed, you would just set the hypothetical $dbConn->connError = TRUE; flag in the try block.

    So, to summarize, while the specifics of how to structure the actual code depend on exactly what you're doing, using exceptions to handle error conditions is IMHO the most robust way to go about it.

    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用