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

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

问题描述

我总是假定,无论数据库/ ORM,像mysql_close(),Propel :: close()等,都是一个很好的关闭数据库连接的方法。



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



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



有人可以在下面什么情况下,我们应该使用close方法关闭DB连接? &如果这些方法根本没用,为什么它们存在于所有db libs / ORM?



EDIT



我的结论



这是Bondye和Fluffeh之间的一个很好的讨论,连接关闭。非常感谢两者。




  • 如果您的脚本预计持续时间少于100毫秒,请不要关闭连接。

  • 但是:如果脚本预期持续更长时间,并且在最后一个数据库操作和关闭脚本之间有一段时间,通过调用* close()为其他人免费连接。



我真的很难接受一个答案,因为两个答案在它的地方是正确的。只接受回答所有的评论,以便它保持在顶部。 c>使用 mysql_close()



释放资源


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


希望这可以帮助你更多。



来源



编辑



mysql_close()也是为了节省计算机资源,但使用它的另一个关键原因是因为MySQL服务器可以接受有限数量的连接,如果你有几个客户端保持连接打开没有理由,然后服务器可能很需要关闭其他,等待客户端。自然这是一件坏事,因此,和 mysql_free_result()一样,调用 mysql_close() if你认为上次数据库使用和脚本结束之间会有一些时间。


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天全站免登陆