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

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

现在问题是,用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

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

18个答案

默认排序 按投票排序
0

Output

Whether it is parody type apparel, original created tributes, Parody, Hillman College and spreading positive messages, or based off television or movies Our wide variety of apparel, merchandise, and memorabilia will not disappoint! Discover More at faithworkapparel.com

0

There are many services that offer to teach non native speakers how to speak English The problem with many of them is that they don’t focus on real English conversations This can make the difficult task of learning to speak a new language. Listening English Practice on our website can help you to learn english fast and effectively.Learn English fast with the Effortless English courses and improve your speaking skills online.

0

Muktupolis Safety Playground 토토사이트 Major Site Private Toto Safety Park Sports Site is the best Toto verification site that filters the Muktu sites that you can trust and use.

0

We're on a mission to make legal skills training engaging, effective, easy to use—and accessible to all. Lawyer Skills

0

Cape Cod Pool Repair | Pool Remodeling and Plastering Cape Cod Pools

0

Find the Maldives Holidays Package & right Luxury hotel for your honeymoon or Holidays Rawnaq Tourism recommends the Best MaldivesM Hotels depending on your individual needs

0

Class Companion | Class Attendance App | Class Management Software Attendance App for Teachers

0

Pool Plaster Inc. | quartz plaster | Pool Renovation Services Boston quartzscape

0

Somebody essentially assist to make seriously posts I'd state. That is the very first time I frequented your web page and so far? I amazed with the analysis you made to create this actual put up amazing. Excellent job!

ปลากัดสวยงาม | betta fish for sale | เมล็ดกาแฟ

0

In addition, there are Para-Okazino, Coin Casino, Sands Casino, Okada Casino, Merit Casino, First Casino, Duzon Casino, and SM Casino. 카지노사이트

0

I don't like your toys Cypher, don't spy on me.

0

Get the latest breaking and in-depth news,headlines,photos and videos on radical Islamic terrorist groups. Whether you are Muslim or not your input is vital.For any information,ideas,opinions contact us or write us @ shaniathura@gmail.com International Politics News

0

As America's #1 Timeshare Exit Resource. We have helped NUMEROUS of owners just like you. Don't stay stuck in a money-pit timeshare contract, reach out to us regarding ALL your questions. newton group transfers reviews

0

I read your blog frequently and I just thought I’d say keep up the amazing work! 먹튀사이트

0

Just by saying I love youuuuu

0

Extremely helpful post. This is my first time i visit here. I discovered such a large number of intriguing stuff in your blog particularly its exchange. Truly its extraordinary article. Keep it up. https://first-11.net/majorsite_1/

0

hanks for Nice and Informative Post. This article is really contains lot more information about This Topic. https://gamebnat.net/mtsite09/

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