php 通过 pdo 长连接 链接pgsql 但是pgsql连接数会不断增加 而不是复用一个链接

__都付笑谈中 2016-07-15 04:32:42
现在问题是,用pdo的做的长连接链接的pgsql,但是每次访问页面pgsql的链接数都会不断增加,而不是去用缓存好的连接,并且当一个用户操作时会涨到11个左右 就不会继续增加,然后再多一个用户访问连接数又会增加增加到19个左右又不增加了,再增加个用户会涨到20多个。求大神们支援

//数据库链接代码 db.class.php

/**
* 数据库构造函数
*/
public function __construct() {
// $this->memcache = $this->getMemecacheObject();
/** 单一实例 PDO对象 */
if (!self::$PDOInstance) {
$this->config = json_decode(file_get_contents(ROOT_ADDR."/private/config/db.json"), true);

$config = $this->config;
$host = $config["data_base"]["db_host"];
$dbname = $config["data_base"]["db_name"];
$port = $config["data_base"]["db_port"];
$username = $config["data_base"]["db_user"];
$password = $config["data_base"]["db_pwd"];

if ($config["data_base"]["db_host"] != 'localhost') {
$hosturl = "host=$host;";
}

try
{
self::$PDOInstance = new PDO("pgsql:"
. $hosturl
. "port=$port;"
. "dbname=$dbname;"
, $username
, $password
, array(
PDO::ATTR_PERSISTENT => true,
)
);
} catch (Exception $ex) {
$tools = new tools();
$path = $tools->log("数据库初始化失败,已强制断开链接。<br/>抓取到的异常栈如下:<br/><pre>" . print_r($ex, true) . "</pre>", 'db');

header("Content-type:text/html;charset=utf-8");
if ($config["SYSTEM"]["DEBUG"]) {
echo "数据库初始化失败,已强制断开链接。<br/>详细信息请访问{$path}文件日志";
} else {
echo "数据库初始化失败,请联系系统管理员。";
}
die();
}
try {
self::$PDOInstance->query("SET client_encoding='UTF-8';");
self::$PDOInstance->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
self::$PDOInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $exc) {
$this->pdo=NULL;
$doc = <<<DOC
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="layer/jquery-1.11.1.min.js"></script>
<script src="layer/layer.js"></script>
<head>
<body>
DOC;
print $doc;
$info=L('服务器变更请刷新');
print("<script>layer.msg('".$info."', {icon: 2,time: 30000},function(){location.reload();});</script>");
print('</body></html>');
self::$PDOInstance=NULL;
exit();
}
}


$this->pdo = self::$PDOInstance;
$this->getconnect_server();
}

//每个模块需要做数据库操作时,model层会去执行下父类(db.class.php)的构造方法 然后进行数据库操作

public function __construct($data) {
parent::__construct();
}

[root@asg omp]# ps -aux | grep post | grep omp
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
postgres 31717 0.1 0.2 199876 8864 ? Ss 16:25 0:00 postgres: ompuser OMPDB asg.localdomain(43255) idle
postgres 31725 0.2 0.2 200032 9964 ? Ss 16:25 0:00 postgres: ompuser OMPDB asg.localdomain(43256) idle
postgres 31787 0.0 0.1 199568 5496 ? Ss 16:25 0:00 postgres: ompuser OMPDB [local] idle

[root@asg omp]# ps -aux | grep post | grep omp
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
postgres 31717 0.0 0.2 199876 8864 ? Ss 16:25 0:00 postgres: ompuser OMPDB asg.localdomain(43255) idle
postgres 31725 0.1 0.2 200032 9964 ? Ss 16:25 0:00 postgres: ompuser OMPDB asg.localdomain(43256) idle
postgres 31787 0.0 0.1 199568 5496 ? Ss 16:25 0:00 postgres: ompuser OMPDB [local] idle
postgres 32234 0.2 0.2 199924 8352 ? Ss 16:25 0:00 postgres: ompuser OMPDB asg.localdomain(43257) idle
postgres 32254 0.1 0.1 199724 7516 ? Ss 16:25 0:00 postgres: ompuser OMPDB asg.localdomain(43258) idle

[root@asg omp]# ps -aux | grep post | grep omp
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
postgres 2954 0.0 0.1 199660 6752 ? Ss 16:30 0:00 postgres: ompuser OMPDB asg.localdomain(43261) idle
postgres 2956 0.0 0.1 199724 7496 ? Ss 16:30 0:00 postgres: ompuser OMPDB asg.localdomain(43262) idle
postgres 2966 0.2 0.2 200216 8632 ? Ss 16:30 0:00 postgres: ompuser OMPDB asg.localdomain(43263) idle
postgres 2967 0.0 0.1 199660 6748 ? Ss 16:30 0:00 postgres: ompuser OMPDB asg.localdomain(43264) idle
postgres 2968 0.0 0.1 199660 6760 ? Ss 16:30 0:00 postgres: ompuser OMPDB asg.localdomain(43265) idle
postgres 31717 0.0 0.2 200068 9144 ? Ss 16:25 0:00 postgres: ompuser OMPDB asg.localdomain(43255) idle
postgres 31787 0.0 0.1 199568 5496 ? Ss 16:25 0:00 postgres: ompuser OMPDB [local] idle
postgres 32234 0.0 0.2 199924 8352 ? Ss 16:25 0:00 postgres: ompuser OMPDB asg.localdomain(43257) idle
postgres 32254 0.0 0.1 199724 7516 ? Ss 16:25 0:00 postgres: ompuser OMPDB asg.localdomain(43258) idle

...全文
353 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复

20,359

社区成员

发帖
与我相关
我的任务
社区描述
“超文本预处理器”,是在服务器端执行的脚本语言,尤其适用于Web开发并可嵌入HTML中。PHP语法利用了C、Java和Perl,该语言的主要目标是允许web开发人员快速编写动态网页。
phpphpstorm 技术论坛(原bbs)
社区管理员
  • 开源资源社区
  • phpstory
  • xuzuning
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧