关闭mysql连接重要吗? [英] Is closing the mysql connection important?

查看:116
本文介绍了关闭mysql连接重要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

明智地关闭mysql连接效率是否至关重要,还是在php文件运行后自动关闭?

Is it crucial to close mysql connections efficiency wise, or does it automatically close after php file has run?

推荐答案

来自文档:

注意:脚本执行结束后,将立即关闭到服务器的链接,除非更早地通过显式调用mysql_close()关闭了该链接.

Note: The link to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling mysql_close().

如果脚本在提取结果后要执行大量处理,并且已检索到完整的结果集,则绝对应该关闭连接.如果您不这样做,那么当Web服务器使用率过高时,MySQL服务器就有可能达到其连接限制.如果直到脚本结束之前都无法关闭MySQL连接,尽管这样做不必显式进行,这样做会更清洁.

If your script has a fair amount of processing to perform after fetching the result and has retrieved the full result set, you definitely should close the connection. If you don't, there's a chance the MySQL server will reach it's connection limit when the web server is under heavy usage. If you can't close the MySQL connection until near the end of the script, it's cleaner though unnecessary to do so explicitly.

我不确定fastcgi如何影响事物. 一页声称支持fastcgi的PHP构建将创建持久性连接,即使对于mysql_connect也是如此.这与文档相矛盾,因为该文档将在进程(而不是脚本)结束时关闭连接.我将建议使用mysql_close()而不是对其进行测试.实际上,我建议使用 PDO (如果可用).

I'm not certain how fastcgi affects things. One page claims that a build of PHP that supports fastcgi will create persistent connections, even for mysql_connect. This contradicts the documentation in that the connection is closed when the process, rather than the script, ends. Rather than testing it, I'm going to recommend using mysql_close(). Actually, I recommend using PDO, if it's available.

这篇关于关闭mysql连接重要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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