乌有 发表于 2010-5-18 18:16:54

关于数据库查询连接关闭问题

公司服务器管理员说服务器数据库连接这边压力过高
经监测,说是项目数据库连接进程过多没有自动关闭.
数据库类是用自动加载资源,即设置了config/autoload.php
我修改了CI默认的开启持久连接为FALSE,即$db['test']['pconnect'] = FALSE;
不过没有改善.
据自己的理解,采用mysql_connect是在页面运行后会自动关闭链接.
至于说没有关闭,就不知道如何入手了.
公司就我自己用框架,说平时没有出现过这种情况.让我排查.
但不觉得是CI的不足所致.
上来看看各位对此有什么看法.

Hex 发表于 2010-5-18 23:12:44

PHP 会在执行完毕自动释放资源,我认为没有任何方式能阻止 PHP 释放资源(P链接除外)。
CI 也是调用的 PHP 的函数,不可能出现 CI 干预 PHP 内部功能的情况。

乌有 发表于 2010-5-19 14:39:48

昨天关闭了配置文件中的持久连接,今天的监测中说连接进程正常.
数据库配置文件的更改会不会出现缓存现象,当天好像并不即时.
也不知道怎么解释,应该脚本是即读即用吧.

Hex 发表于 2010-5-19 18:12:45

那应该是以前的进程还没有关闭,你即使取消了 P 连接,但以前的进程还可能存在 -_-

saturn 发表于 2010-5-19 21:06:31

PHP在CGI模式下本身的垃圾回收机制很简单,就如同Hex在二楼所说:当scripts运行完毕,释放消耗过的系统资源。但pconnection是个例外。

我想LZ应该是碰到了下面这个问题,看最后一条:

http://www.php.net/manual/en/function.mysql-pconnect.php#85670

Apache does not work well with persistent connections. When it receives a request from a new client, instead of using one of the available children which already has a persistent connection open, it tends to spawn a new child, which must then open a new database connection. This causes excess processes which are just sleeping, wasting resources, and causing errors when you reach your maximum connections, plus it defeats any benefit of persistent connections. (See comments below on 03-Feb-2004, and the footnote at http://devzone.zend.com/node/view/id/686#fn1)

我就不翻译了,比较简单,大家应该都看得懂。毕竟都是用CI这个国外框架的。

PHP官方对持久化连接的阐述:http://www.php.net/manual/en/features.persistent-connections.php
页: [1]
查看完整版本: 关于数据库查询连接关闭问题