Zend“析构函数”?
我们使用MVC 的Zend 框架和Pear 来访问数据库。我想知道每次请求完成时是否应该关闭与数据库的连接?如果是这样,有没有办法优雅地做到这一点?
我们有大约 50 个操作分布在多个控制器上,我真的不想将 db->disconnect() 放在每个控制器的末尾。有没有一种方法可以让我将此断开连接命令放在一个位置(如果愿意的话,可以将其称为“析构函数”),并让 Zend 在请求完成时自动调用此命令?
We use the Zend framework for MVC and Pear for accessing the database. I was wondering should I close the connection to the database everytime a request is completed? And if so is there a way to do this elegantly?
We have about 50 actions spread across multiple controllers, and I don't really want to put db->disconnect() at the end of each controller. Is there a way that I can put this disconnect command in one location, a "destructor" if you will, and have Zend call this automatically whenever a request is completed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前我可以想到你可以考虑的两种可能性。
首先,您可以将“析构函数”放入控制器插件中例如在dispatchLoopShutdown钩子中。
其次,如果您确实希望在请求的最后执行某些操作,您可以将“析构函数”放在
$application->bootstrap()->run();
之后索引.php。At the moment I can think of two possibilities that you could consider.
First, you could put your "destructor" in a controller plugin in e.g. dispatchLoopShutdown hook.
Second, if you really want something to be executed at the very end of you request, you could put your "destructor" after
$application->bootstrap()->run();
in your index.php.只有 100% 安全的解决方案是使用
register_shutdown_function()
。即使请求被重定向($this->_helper->redirector->gotoUrlAndExit()
)或脚本被终止(使用die()
或 <代码>exit())。插件和索引的最后一行都可能在重定向或终止脚本时失败。Only 100% bulletproof solution is using
register_shutdown_function()
. It's called even if request is redirected ($this->_helper->redirector->gotoUrlAndExit()
) or script is killed (usingdie()
orexit()
). Both - plugin and index's last line can fail on redirect or killed script.