我真的需要做mysql_close()吗? [英] Do I really need to do mysql_close()

查看:93
本文介绍了我真的需要做mysql_close()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的需要做mysql_close()吗?为什么或为什么不呢?

Do I really need to do mysql_close()? Why or why not?

即使我不执行mysql_close,也有触发器在mysql_connect之后关闭链接吗?

Is there a trigger that closes the link after mysql_connect even if I don't do mysql_close?

推荐答案

在大多数情况下,调用mysql_close不会对性能产生任何影响.但是,关闭程序不再使用的资源(文件句柄,打开的套接字,数据库连接等)始终是一个好习惯.

In most cases calling mysql_close will not make any difference, performance-wise. But it's always good practice to close out resources (file handles, open sockets, database connections, etc.) that your program is no longer using.

如果您做的事情可能要花几秒钟的时间(例如,从REST API读取和解析数据),则尤其如此.由于API调用正在进行中,因此负面的网络状况可能会导致脚本阻塞几秒钟.在这种情况下,在 REST调用完成并解析之后,打开数据库连接的适当时间为.

This is especially true if you're doing something that may potentially take a few seconds - for instance, reading and parsing data from a REST API. Since the API call is going over the line, negative network conditions can cause your script to block for several seconds. In this case, the appropriate time to open the database connection is after the REST call is completed and parsed.

总结一下,两个主要规则是:

To sum up my answer, the two big rules are:

  1. 仅在程序准备使用它们时分配资源(文件句柄,套接字,数据库连接等).
  2. 程序完成后立即释放资源.

这篇关于我真的需要做mysql_close()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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