“ping”是个好主意吗?当我的应用程序引导时我的数据库?
但是,比如说,在我的数据库实例化插件中,我有这样的代码:
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
$db = Zend_DB::factory("Pdo_Mysql", $this->config);
Zend_Registry::set("db", $db);
}
即使数据库未连接,也不会抛出任何错误。这是因为 Zend_DB
不会费心检查数据库是否处于活动状态,以免浪费资源和时间。
我的问题是我是否应该这样做:
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
$db = Zend_DB::factory("Pdo_Mysql", $this->config);
$db->getConnection();
Zend_Registry::set("db", $db);
}
这样,任何连接问题都会尽早被发现并显示出来。这是个好主意吗?我的应用程序是否应该等待检查数据库连接在第一次使用时是否处于活动状态?
One of my recent questions (here) asked about how to catch errors (and display them with the ErrorController) in the Bootstrap file. The example I used was during database instantiation: if an error occurred in the Bootstrap, it would just dump the error onto the screen without calling the ErrorController.
But, say, in my database instantiation plugin, I have this code:
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
$db = Zend_DB::factory("Pdo_Mysql", $this->config);
Zend_Registry::set("db", $db);
}
No error will be thrown, even if the database isn't connected. This is because Zend_DB
doesn't bother to check that the database is live, so as to not waste resources and time.
My question is whether I should do something like this:
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {
$db = Zend_DB::factory("Pdo_Mysql", $this->config);
$db->getConnection();
Zend_Registry::set("db", $db);
}
That way, any connectivity problems are caught early on and displayed. Is this a good idea? Should my application instead wait to check that the database connection is live on first usage?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将在错误控制器中设置 Zend_Db 异常类型的特定处理。
PHP 不是有状态的,因此每个页面请求都会发生应用程序的初始化。如果我的页面请求甚至不需要数据库(例如我的维护页面、错误页面等)怎么办?
最重要的是,引导期间无连接的正确处理是什么?如何确定渲染错误页面所需的任何其他资源已完成引导?
然而,这只是我的偏好。
I would setup specific handling of the Zend_Db exception type in my error controller.
PHP is not stateful, so initialization of the app is going to happen for every page request. What if my page request doesnt even need the DB (for example, my maintenance page, error pages, etc)?
Most importantly, what is the proper handling of no connection during bootstrap? How can you be sure that any other resources you need in order to render an error page have finished bootstrapping?
However, this is just my preference.