我应该什么时候关闭数据库连接? [英] When should I close the db connection?

查看:75
本文介绍了我应该什么时候关闭数据库连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否必须在 PHP 脚本中关闭连接?

Is it a must to close the connection in PHP script?

推荐答案

根据您的数据库服务器的配置,可能同时打开的连接数是有限制的.

Depending on the configuration of your DB server, there is a limit on the possible number of connections opened to it at the same time.

所以,如果你的脚本:

  • 做一些查询
  • 然后,在不做任何查询的情况下进行一些长时间的计算

在完成所有查询后关闭连接可能会很有趣 - 并且仅在需要时才打开连接.

It can be interesting to close the connection after having done all your queries -- and to only open the connection when it's becoming needed.


不过,请注意,无论如何,脚本结束时连接都会关闭;这意味着,如果您没有 wya 来确保您已完成查询,则您不需要需要关闭连接:保持打开状态允许您在任何时候进行一些额外的查询必要的.


Still, note that connections are closed when the script ends, anyway ; which means that if you don't have a wya to be sure that you have finished doing queries, you don't need to close the connection : keeping it opened allows you to do some additionnal queries whenever it's necessary.

(尤其是您的页面是使用几个不同且独立的组件构建的,这些组件都容易进行数据库查询)


对于我写的应用程序,我一般:


For the applications I write, I generally :

  • 在第一次查询时打开连接(这意味着如果没有发送查询则不会打开连接)
  • 永远不要关闭连接:因为我的页面是使用大量组件构建的,所以我无法确定不再需要连接.

这篇关于我应该什么时候关闭数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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