简体   繁体   English

不关闭PHP脚本中的mysql连接的缺点是什么?

[英]what is the downside of not closing a mysql connection in php script?

I am accessing a database in a php script by establishing a connection using mysqli_connect function. 我通过使用mysqli_connect函数建立连接来访问php脚本中的数据库。 I observe it is not mandatory to close the connection at the end of the script. 我观察到在脚本末尾关闭连接不是强制性的。

What are the implications of not using mysqli_close() in a connection script created to access mysql database in php? 在为访问php中的mysql数据库而创建的连接脚本中不使用mysqli_close()有什么含义?

If you are using cgi, then it is not necessary to close your mysql connections since they close automatically at the end of script execution. 如果使用cgi,则不必关闭mysql连接,因为它们会在脚本执行结束时自动关闭。

From the documentation : 文档中

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

Although it is considered a good practice to close your connection. 尽管关闭连接被认为是一个好习惯。

If you close the connection yourself: 如果您自己关闭连接:

  • You have to check the value of $_connected for every single query. 您必须为每个查询检查$_connected的值。 This means PHP has to check that the variable $_connected A) exists B) is a boolean and C) is true/false. 这意味着PHP必须检查变量$_connected A)是否存在B)是布尔值并且C)是/ $_connected
  • You have to call your 'disconnect' function, and function calls are one of the more expensive operations in PHP. 您必须调用“断开连接”函数,而函数调用是PHP中较昂贵的操作之一。 PHP has to check that your function A) exists, B) is not private/protected and C) that you provided enough arguments to your function. PHP必须检查您的函数A)是否存在,B)是否不是私有的/受保护的以及C)您是否为函数提供了足够的参数。 It also has to create a copy of the $connection variable in the new local scope. 它还必须在新的本地范围中创建$ connection变量的副本。
  • Then your 'disconnect' function will call mysql_close() which means PHP A) checks that mysql_close() exists and B) that you have provided all needed arguments to mysql_close() and C) that they are the correct type (mysql resource). 然后,您的“断开连接”函数将调用mysql_close(),这意味着PHP A)检查mysql_close()是否存在,以及B)您已向mysql_close()和C)提供了所有必需的参数,它们是正确的类型(mysql资源)。

So if you are not using persistent connections, your MySQL connection will be closed at the end of the page execution. 因此,如果您不使用持久连接,则在页面执行结束时将关闭MySQL连接。 So you dont have to bother about that. 因此,您不必为此而烦恼。 And hence no downsides. 因此没有任何缺点。

If you're fairly sure you're not going to use the connection again, or don't have a class that manages your open connections, closing is good practice. 如果您确定不会再次使用该连接,或者没有用于管理打开的连接的类,则关闭是一个好习惯。 A couple reasons: 有两个原因:

  1. If you're looping or something over something that creates connections without closing prior ones, you could eat up all your available DB connections based on whatever limit is set on the sql servers side. 如果您正在循环或在某种程度上创建了连接而不关闭先前的连接,则可以根据sql服务器端设置的任何限制来耗尽所有可用的数据库连接。 Typically this limit is for everyone not per host, so you could prevent others from connecting as well. 通常,此限制是针对不是每个主机的每个人的,因此您也可以阻止其他人连接。
  2. From Zend (maybe dated): Open connections (and similar resources) are automatically destroyed at the end of script execution. 从Zend(可能已过时)中:在脚本执行结束时,会自动销毁打开的连接(和类似资源)。 However, you should still close or free all connections, result sets and statement handles as soon as they are no longer required. 但是,您仍然应该在不再需要它们时关闭或释放所有连接,结果集和语句句柄。 This will help return resources to PHP and MySQL faster. 这将有助于更快地将资源返回到PHP和MySQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM