Uploaded image for project: 'PHP Legacy Driver'
  1. PHP Legacy Driver
  2. PHP-1115

Simplify the connection mechanism with persistent connections



    • Type: New Feature
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Won't Fix
    • Affects Version/s: 1.5.1
    • Fix Version/s: 2.0.0
    • Component/s: pecl-mongo
    • Labels:
    • Backwards Compatibility:
      Fully Compatible
    • # Replies:
    • Last comment by Customer:


      As per PHP-854, Hannes Magnusson said that what is found on http://www.php.net/manual/en/class.mongoclient.php:

      $m = new MongoClient(); // connect
      $db = $m->foo; // get the database named "foo"

      isn't the best way to connect and that something like:

      function getMongoClient($seeds = "", $options = array(), $retry = 3) {
          try {
              return new MongoClient($seeds, $options);
          } catch(Exception $e) {
              /* Log the exception so we can look into why mongod failed later */
          if ($retry > 0) {
              return getMongoClient($seeds, $options, --$retry);
          throw new Exception("I've tried several times getting MongoClient.. Is mongod really running?");
      try {
          $mc = getMongoClient("localhost", array());
      } catch(Exception $e) {
          /* Can't connect to MongoDB! */
          die("Can't do anything :(");

      should be used instead.

      While I understand the technical reason behind this, I think it would be easier for PHP developers that this mechanism would be done by the PHP driver internally.

      There is various possibilities for this:

      • Implement auto reconnect, like with mysqli/mysqlnd extensions, possibly with an internal retry counter that is reset upon success.
      • Implement a retry loop internally (while connecting), configurable with a setting like: mongo.connect_retries

      The advantages are:

      • PHP devs don't have to re-implement it every time
      • PHP devs don't have to figure out why they get "Remote server has closed the connection" and that it's normal behavior
      • Connection details/mgmt handled at the same level that persistence is. (=> driver level instead of user land)
      • By implementing automatic re-connection, it would even be possible to have PHP applications that are more robust in case of network issues, especially with long time running scripts, like cronjobs, queuing solutions,... that migth use

        new MongoClient()

        only at startup.


          Issue Links



              • Votes:
                3 Vote for this issue
                5 Start watching this issue


                • Created:
                  Days since reply:
                  5 years, 4 weeks ago
                  Date of 1st Reply: