php 是否有必要关闭 PDO 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15444748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:14:34  来源:igfitidea点击:

Is it necessary to close PDO connections

phppdo

提问by Rujikin

I noticed there is no close function for PDO. Should I close the connection or is it unnecessary for PDO?

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

回答by Stegrex

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.

成功连接到数据库后,PDO 类的实例将返回到您的脚本。该连接在该 PDO 对象的生命周期内保持活动状态。要关闭连接,您需要通过确保删除对它的所有剩余引用来销毁该对象——您可以通过将 NULL 分配给保存该对象的变量来完成此操作。如果您没有明确地这样做,PHP 将在您的脚本结束时自动关闭连接。

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

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 对象设置为 null。

回答by PeeHaa

This question is depending a bit on the type of project and the type of connection.

这个问题有点取决于项目类型和连接类型。

In almost all of my projects I never manually close the connection. In PHP the connection (unless it is a persistent connection) will only be open during the request. So manually closing it is pretty useless anyway.

在我几乎所有的项目中,我从不手动关闭连接。在 PHP 中,连接(除非它是持久连接)只会在请求期间打开。所以无论如何手动关闭它是非常无用的。

When looking at my projects where there was no persistent connection it would have been veryhard to know when to manually close the connection either way. Once a project gets larger than a couple of files (and the individual components have no idea about eachother like they should) it becomes very hard to know when the connection will still be needed.

当我寻找的项目因不存在持续性方面,它本来是辛苦时,手动关闭连接两种方式就知道了。一旦项目变得比几个文件大(并且各个组件不知道彼此应该如何),就很难知道何时仍需要连接。

And opening the connection again when needed is waay more expensive than just leaving it open during the request.

并且在需要时再次打开连接比在请求期间保持打开更昂贵。

Something though when working with persistent connection there will be situations where you will want to manually close the connection.

但是在使用持久连接时,有些情况下您会想要手动关闭连接。

So to answer your question:

所以回答你的问题:

I noticed there is no close function for PDO.

我注意到 PDO 没有关闭功能。

You can nullify the object reference (and all references to the object) to manually close the connection in PHP.

您可以取消对象引用(以及对对象的所有引用)以手动关闭 PHP 中的连接。

Should I close the connection or is it unnecessary for PDO?

我应该关闭连接还是 PDO 不需要?

In most situations it is notnecessary.

在大多数情况下,这是没有必要的。

回答by hjpotter92

From the PDO's connectionpage

从 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.

成功连接到数据库后,PDO 类的实例将返回到您的脚本。该连接在该 PDO 对象的生命周期内保持活动状态。要关闭连接,您需要通过确保删除对它的所有剩余引用来销毁该对象——您可以通过将 NULL 分配给保存该对象的变量来完成此操作。

如果您没有明确地这样做,PHP 将在您的脚本结束时自动关闭连接。



EDIT

编辑

I'd rather use persistent connection. Though, it's a good practice to close all connections at the end of the script.

我宁愿使用持久连接。不过,在脚本结束时关闭所有连接是一个很好的做法。