2017-08-28 181 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'); 
    . . . 

    ?>