是否需要关闭PDO连接 [英] Is it necessary to close PDO connections

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

问题描述

我注意到PDO没有关闭功能.我应该关闭连接还是不需要PDO?

解决方案

成功连接到数据库后,PDO类的实例将返回到脚本.该连接在该PDO对象的生存期内保持活动状态.要关闭连接,您需要通过确保删除所有剩余的对该对象的引用来销毁该对象,方法是将NULL分配给保存该对象的变量.如果您未明确执行此操作,则PHP将在脚本结束时自动关闭连接.

http://php.net/manual/en/pdo.connections.php

因此答案是否定的,除非您出于任何原因需要在脚本执行期间显式关闭连接,否则无需执行任何操作,在这种情况下,只需将PDO对象设置为null.

I noticed there is no close function for PDO. Should I close the connection or is it unnecessary for 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.

http://php.net/manual/en/pdo.connections.php

So the answer is no, you don't need to do anything unless you need to explicitly close the connection during the script execution for whatever reason, in which case just set your PDO object to null.

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

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