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

0
PHP C/C++ Go ico 602 次浏览

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

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

PHP code?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

/**

 * 数据库构造函数

 */

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)的构造方法 然后进行数据库操作

PHP code?

1

2

3

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

请尽量让自己的答案能够对别人有帮助

1个答案

默认排序 按投票排序
0

Similar disadvantages exist utilizing PDO likewise with some other PHP database interface that does tireless associations: if your content ends out of the blue amidst database activities, the following solicitation that gets the left over association will get the last known point of interest. The association is held open at the procedure chief level, not at the PHP level, and PHP doesn't advise the parent procedure to give the association a chance to kick the bucket when the content ends anomalous.
refer: Dissertation writing service