• 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

    相似問題

    相關經驗

    相關資訊

    相關文檔

  • sesese色