mysql 连接未关闭 查找_MySQL数据库连接未关闭:会发生什么?

bd96500e110b49cbb3cd949968f18be7.png

I am using PHP to query the MySQL database on my website. Please answer the following questions:

What will happen if I don't use mysql_close() when I am done with querying the database in the end? The connection will remain open? If yes then upto how much time? If no then why?

If I open and close a connection to MySQL at several places in a

webpage, how is the performance affected? i.e. connection is made again everytime some access to database is required on a single webpage.

How is mysql_close() related to performance? Should I open a new connection everytime some access to database is required OR should I keep only one connection and close it in the end?

If I don't close the connection, then if the user is trying to

access some data again, will the new connection be used or the old

open connection will be used?

解决方案It will automatically close when the PHP script is done running during destruct phase.

Performance will negatively be affected. Opening a new socket (especially to an external database server) is more expensive and time consuming than just keeping a pointer to the current connection in memory.

See answer no. 2

The access to the data will be performed by a new PHP request. Hence, you will have a new database connection. No problems there.

I'd advise to open your database connection during construct phase, re-use that connection during the entire execution of your script (if it's OO based, assign a class variable for your database connection and use $this->db during the entire script), and close it during destruction (or don't bother at all closing it, as it will be closed anyway, even when not declared specifically).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值