大家伙们,PHP数据库连接是不是在页面执行完之后自己释放,即使不用mysql_close()也可以释放掉?
同理PDO连接到数据库后,是不是非要将PDO实例赋值为NULL,才能将PDO连接断掉?在N久以前我查过资料,但己经模糊不清了,具说PHP处理每一个被请求的页面时,里面有MYSQL连接的,当页面处理完就立刻释放掉所有用到的资料。如:mysql_connect后的连接。这个说法对吗?PDO同理吗?
同理PDO连接到数据库后,是不是非要将PDO实例赋值为NULL,才能将PDO连接断掉?在N久以前我查过资料,但己经模糊不清了,具说PHP处理每一个被请求的页面时,里面有MYSQL连接的,当页面处理完就立刻释放掉所有用到的资料。如:mysql_connect后的连接。这个说法对吗?PDO同理吗?
mysql_close() closes the non-persistent connection to the MySQL server that's associated with the specified link identifier. If link_identifier isn't specified, the last opened link is used.Using mysql_close() isn't usually necessary, as non-persistent open links are automatically closed at the end of the script's execution. See also freeing resources.mysql_close对于非永久的connect不是必须的。
恩,mysql_pconnect 是必须要关闭的这个理解。那在细节上呢?页面处理完毕释放所有资源这样的说法看来是正确的,我相信phper们一定详细整理过。有以前的帖子没?
可以去看下php手册
上面说了mysql_connect() 函数打开非持久的 MySQL 连接。注释:脚本一结束,到服务器的连接就被关闭,除非之前已经明确调用 mysql_close() 关闭了。
也就是说,当脚本结束时,会自动关闭。
pdo 呢,PDO的原理也是一样的吗?
来自手册:Upon successful connection to the database, an instance of the PDO class is returned to your script. The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted--you do this by assigning NULL to the variable that holds the object. If you don't do this explicitly, PHP will automatically close the connection when your script ends.
亲,这么说mysql_pconnect打开的连接用mysql_close关不掉的?@~诧异,那如果我想关掉呢?怎么关?那mysql_connect打开的连接会在脚本执行完毕后由SQL服务器自动关闭喽?
亲,PDO也是这样的吗》?
1、空闲时间超过了mysqld的wait_timeout(单位是秒)
2、web server进程退出,以nginx+php-fpm为例,就是一个php-fpm进程结束
我最近研究此问题,网上搜资料。得知如下情况。
当你用一个浏览器看一个PHP网页时,网站没有被载入完毕,你点了停止按钮或其它情况终止了请求时,好像这个连接就不会被关了呢。还搜到关于 ignore_user_abort() 这个函数。
http://www.cnblogs.com/funlake/archive/2011/09/08/2171822.html