2017-08-28 223 views
-1

我想以類似於java的方式在PHP中實現連接池。Php - 將連接池連接到Mysql

爲什麼我需要這樣的:

讓我們考慮一個流動

Step1: Connection To Db --- Resource Id #12 
Step2: some computation... time taking .3 seconds 
Step3: Query on Solr .... timing taking 2 seconds 
Step4: Connection To Db --- Resource Id #12 (i am using same resource id) 
Step5: Exit 

儘管在第四步:我使用的是相同的數據庫資源作爲第一步的。但是,連接將進入步驟2和步驟3的睡眠狀態,因此在退出之前不能由任何其他PHP進程(其他客戶端)使用。

解決方案:

  1. 使用mysql_close每經過查詢炒魷魚時間:缺點:需要每次,因此耗時

  2. 創建一個Java服務來處理查詢(可以連接,但也耗時,我正在尋找其他解決方案,我需要遷移查詢)

  3. 需要探索像第三方的SQL中繼,但我不確定這會是成功的,並不是很多好公司我用過它

  4. mysql_pconnect沒有解決我的情況。

請建議

+0

您檢查了multiquery? https://www.w3schools.com/php/func_mysqli_multi_query.asp – Will

+0

增加最大連接數... – Salketer

+0

@Salkketer max connection如何幫助我重新使用睡眠連接。增加最大連接數是最差的操作 – chicharito

回答

0
One way that you can apply scalability techniques to this pool model is to allow on the fly changes to your pool distribution. If you have a particular permalink that is extremely popular for some reason, you could move slaves from the primary pool to the comments pool to help it out. By isolating load, you’ve managed to give yourself more flexibility. You can add slaves to any pool, move them between pools, and in the end dial-in the performance that you need at your current traffic level. 

There’s one additional benefit that you get from MySQL database pooling, which is a much higher hit rate on your query cache. MySQL (and most database systems) have a query cache built into them. This cache holds the results of recent queries. If the same query is re-executed, the cached results can be returned quickly. 

If you have 20 database slaves and execute the same query twice in a row, you only have a 1/20th chance of hitting the same slave and getting a cached result. But by sending certain classes of queries to a smaller set of servers you can drastically increase the chance of a cache hit and get greater performance. 

You will need to handle database pooling within your code - a natural extension of the basic load balancing code in Part 1. Let’s look at how we might extend that code to handle arbitrary database pools: 
<?php 

    class DB { 
     // Configuration information: 
     private static $user = 'testUser'; 
     private static $pass = 'testPass'; 
     private static $config = array(
      'write' => 
       array('mysql:dbname=MyDB;host=10.1.2.3'), 
      'primary' => 
       array('mysql:dbname=MyDB;host=10.1.2.7', 
         'mysql:dbname=MyDB;host=10.1.2.8', 
         'mysql:dbname=MyDB;host=10.1.2.9'), 
      'batch' => 
       array('mysql:dbname=MyDB;host=10.1.2.12'), 
      'comments' => 
       array('mysql:dbname=MyDB;host=10.1.2.27', 
         'mysql:dbname=MyDB;host=10.1.2.28'), 
      ); 

     // Static method to return a database connection to a certain pool 
     public static function getConnection($pool) { 
      // Make a copy of the server array, to modify as we go: 
      $servers = self::$config[$pool]; 
      $connection = false; 

      // Keep trying to make a connection: 
      while (!$connection && count($servers)) { 
       $key = array_rand($servers); 
       try { 
        $connection = new PDO($servers[$key], 
         self::$user, self::$pass); 
       } catch (PDOException $e) {} 

       if (!$connection) { 
        // Couldn’t connect to this server, so remove it: 
        unset($servers[$key]); 
       } 
      } 

      // If we never connected to any database, throw an exception: 
      if (!$connection) { 
       throw new Exception("Failed Pool: {$pool}"); 
      } 

      return $connection; 
     } 
    } 
    // Do something Comment related 
    $comments = DB::getConnection('comments'); 
    . . . 

    ?>