<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/mongodb.architecture.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'mongodb.connection-handling.php',
    1 => 'Connections',
    2 => 'Connection handling and persistence',
  ),
  'up' => 
  array (
    0 => 'mongodb.architecture.php',
    1 => 'Driver Architecture and Internals',
  ),
  'prev' => 
  array (
    0 => 'mongodb.overview.php',
    1 => 'Architecture',
  ),
  'next' => 
  array (
    0 => 'mongodb.persistence.php',
    1 => 'Persisting Data',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/mongodb/architecture.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="mongodb.connection-handling" class="section">
  
  <h2 class="title">Connection handling and persistence</h2>

  
   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <span class="simpara">
     На платформах Unix розширення чутливе до скриптів, які використовують
     системний виклик fork() без виклику exec(). Користувачам пропонується не
     використовувати повторно примірники
     <span class="classname"><a href="class.mongodb-driver-manager.php" class="classname">MongoDB\Driver\Manager</a></span> у відгалужених підпроцесах.
    </span>
   </p></blockquote>


  <div class="section">
   <h2 class="title">Connection and topology persistence (PHP version since 1.2.0)</h2>

   <p class="para">
    All versions of the extension since 1.2.0 persist the
    <a href="https://github.com/mongodb/mongo-c-driver" class="link external">&raquo;&nbsp;libmongoc</a> client object in
    the PHP worker process, which allows it to re-use database connections,
    authentication states, <em>and</em> topology information across
    multiple requests.
   </p>

   <p class="para">
    When <span class="methodname"><a href="mongodb-driver-manager.construct.php" class="methodname">MongoDB\Driver\Manager::__construct()</a></span> is
    invoked, a hash is created from its arguments (i.e. URI string and array
    options). The extension will attempt to find a previously persisted
    <a href="https://github.com/mongodb/mongo-c-driver" class="link external">&raquo;&nbsp;libmongoc</a> client object for
    that hash. If an existing client cannot be found for the hash, a new client
    will be created and persisted for future use. This behavior can be disabled
    via the <code class="literal">&quot;disableClientPersistence&quot;</code> driver option.
   </p>

   <p class="para">
    Each client contains its own database connections and a view of the server
    topology (e.g. standalone, replica set, shard cluster). By persisting the
    client between PHP requests, the extension is able to re-use established
    database connections and remove the need for
    <a href="https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.md" class="link external">&raquo;&nbsp;discovering the server topology</a>
    on each request.
   </p>

   <p class="para">
    Consider the following example:
   </p>

   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$managers </span><span style="color: #007700">= [<br />    new </span><span style="color: #0000BB">MongoDB\Driver\Manager</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://127.0.0.1'</span><span style="color: #007700">),<br />    new </span><span style="color: #0000BB">MongoDB\Driver\Manager</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://127.0.0.1'</span><span style="color: #007700">),<br />    new </span><span style="color: #0000BB">MongoDB\Driver\Manager</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://127.0.0.1:27017'</span><span style="color: #007700">),<br />    new </span><span style="color: #0000BB">MongoDB\Driver\Manager</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://rs1.example.com,rs2.example.com/'</span><span style="color: #007700">, [</span><span style="color: #DD0000">'replicaSet' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'myReplicaSet'</span><span style="color: #007700">]),<br />];<br /><br />foreach (</span><span style="color: #0000BB">$managers </span><span style="color: #007700">as </span><span style="color: #0000BB">$manager</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">$manager</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">executeCommand</span><span style="color: #007700">(</span><span style="color: #DD0000">'test'</span><span style="color: #007700">, new </span><span style="color: #0000BB">MongoDB\Driver\Command</span><span style="color: #007700">([</span><span style="color: #DD0000">'ping' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">1</span><span style="color: #007700">]));<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>


   <p class="para">
    The first two Manager objects will share the same
    <a href="https://github.com/mongodb/mongo-c-driver" class="link external">&raquo;&nbsp;libmongoc</a> client since
    their constructor arguments are identical. The third and fourth objects will
    each use their own client. In total, three clients will be created and the
    PHP worker executing this script will open two connections to
    <code class="literal">127.0.0.1</code> and one connection to each of
    <code class="literal">rs1.example.com</code> and <code class="literal">rs2.example.com</code>.
    If the extension discovers additional members of the replica set after
    issuing <code class="literal">hello</code> commands, it will open additional
    connections to those servers as well.
   </p>

   <p class="para">
    If the same worker executes the script again in a second request, the three
    clients will be re-used and no new connections will be made. Depending on
    how long ago the previous request was served, the extension may need to
    issue additional <code class="literal">hello</code> commands to update its view of the
    topologies.
   </p>
  </div>

  <div class="section">
   <h2 class="title">Socket persistence (PHP versions before 1.2.0)</h2>

   <p class="para">
    Versions of the extension before 1.2.0 utilize PHP&#039;s Streams API for
    database connections, using an API within
    <a href="https://github.com/mongodb/mongo-c-driver" class="link external">&raquo;&nbsp;libmongoc</a> to designate
    custom handlers for socket communication; however, a new libmongoc client is
    created for each <span class="classname"><a href="class.mongodb-driver-manager.php" class="classname">MongoDB\Driver\Manager</a></span>. As a result,
    the extension persists individual database connections but not
    authentication state or topology information. This means that the extension
    needs to issue commands at the start of each request to authenticate and
    <a href="https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.md" class="link external">&raquo;&nbsp;discover the server topology</a>.
   </p>

   <p class="para">
    Database connections are persisted by a hash derived from the server&#039;s
    host, port, and the URI string used to construct the
    <span class="classname"><a href="class.mongodb-driver-manager.php" class="classname">MongoDB\Driver\Manager</a></span>. The constructor&#039;s array
    options are not included in this hash.
   </p>

   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <span class="simpara">
     Versions of the extension &gt;= 1.1.8 and &lt; 1.2.0 do not persist sockets
     for SSL connections. See
     <a href="https://jira.mongodb.org/browse/PHPC-720" class="link external">&raquo;&nbsp;PHPC-720</a> for
     additional information.
    </span>
   </p></blockquote>

   <p class="para">
    Despite its shortcomings with persisting SSL connections when and topology
    information, this version of the extension supports all
    <a href="context.ssl.php" class="link">SSL context options</a> since it uses
    PHP&#039;s Streams API.
   </p>
  </div>
 </div><?php manual_footer($setup); ?>