在php中使用关闭数据库连接 [英] Use of closing database connection in php

查看:127
本文介绍了在php中使用关闭数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是假设不管数据库/ ORM如mysql_close(),Propel :: close()等等都关闭数据库连接总是一个很好的做法。



参考我的另一个问题和其他一些互联网研究,我来了解一个令人惊讶的面孔,大多数人建议,如果您关闭连接并不重要,因为连接总是在请求后关闭。



然而,我发现这些答案很难消化。原因是为什么所有的DB lib,ORM都提供了密切的方法?如果它在那里,每个ORM / lib 中的,必须有一些很好的用处。



有人可以在下面点亮一下在什么情况下,应该使用close方法来关闭DB连接? &安培;如果这些方法根本没有用,为什么它们都存在于所有db lib / ORM中?



编辑



我的结论



这是Bondye和Fluffeh之间的一个很好的讨论,它清除了对于使用连接关闭。感谢他们两人。




  • 如果您的脚本预计持续时间不到100 ms,不要打扰关闭连接。
  • 但是,如果脚本预期持续时间较长,并且在最后一个DB操作和脚本关闭之间有一段时间,可以通过调用* close()来为其他人免费连接。


我很难接受一个答案,因为这两个答案都是正确的。只要接受所有评论的答案,使其保持在顶部。但是对于两个正确的答案都是+1。

解决方案

使用 mysql_close()通常不需要,因为在脚本执行结束时,非持久开放链接会自动关闭。



释放资源


感谢使用PHP 4的Zend Engine引入的引用计数系统,自动检测到一个没有更多引用的资源,它被释放垃圾收集器。因此,很少需要手动释放内存。


希望这可以帮助您更多。



来源



编辑:



mysql_close()也是为了节省计算机资源,但是使用它的另一个关键原因是MySQL服务器可以接受的连接数量有限,如果你有几个客户端无连接地打开连接,服务器可能需要转移其他等待的客户端。当然这是一件坏事,所以,像 mysql_free_result()一样,调用 mysql_close()您认为在最后一个数据库使用和脚本结束之间会有一段时间。


I was always in assumption that it is always a good practice to close database connection, regardless of database/ORM, like mysql_close(), Propel::close() etc.

With reference to one of my other question and some other research on Internet, I came to know a surprising face that most people recommends it doesn't really matter if you close connection as connection always gets closed after the request.

However I'm finding those answers little difficult to digest. Reason is, why all DB lib, ORM provide close method? If it is there, in every ORM/lib, there must be some good use of it.

Can someone please shed some light on under what circumstances, we should use close method to close DB connection? & if these methods are not useful at all, why they are present there in all db libs/ORM?

EDIT

My Conclusion

It was a good discussion between Bondye and Fluffeh and it cleared my doubts about use of connection closing. Thanks to both of them.

  • If your script is expected to last of less than 100 ms, dont bother closing connection.
  • BUT: if script is expected to last longer and there is some time between last DB operation and close of script, free connection for others by calling *close().

It is really very difficult for me to accept one answer as both answer are correct on its place. Just accepting answer with all comments so that it remain on top. But +1 to both correct answers.

解决方案

Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution.

Freeing resources

Thanks to the reference-counting system introduced with PHP 4's Zend Engine, a resource with no more references to it is detected automatically, and it is freed by the garbage collector. For this reason, it is rarely necessary to free the memory manually.

Hope this helps you more.

(source)

edit:

The purpose of mysql_close() is also to save computer resources, but another key reason for using it is because there is a limited number of connections that a MySQL server can accept, and if you have several clients holding connections open for no reason then the server may well need to turn away other, waiting clients. Naturally this is a bad thing, so, as with mysql_free_result(), it is good to call mysql_close() if you think there will be some time between your last database use and your script ending.

这篇关于在php中使用关闭数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆