Sophie

Sophie

distrib > Mageia > 7 > armv7hl > by-pkgid > 2b917e0437961edec048f1d15e2d7449 > files > 9874

php-manual-en-7.2.11-1.mga7.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Persistent Connections (version 1.3.0+)</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.connecting.uds.html">Domain Socket Support</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.connecting.pools.html">Connection Pooling (version 1.2.0-1.2.12 *only*)</a></div>
 <div class="up"><a href="mongo.connecting.html">Connecting</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongo.connecting.persistent" class="section">
  <h2 class="title">Persistent Connections (version 1.3.0+)</h2>

  <p class="para">
   All versions of the driver since 1.3.0 utilize persistent connections to
   minimize the number of connections made to each database server. These
   connections are saved by the PHP worker process and may be reused between
   multiple requests.
  </p>

  <p class="para">
   Before connecting to a database server, the driver will create a hash for the
   connection based on its host, port, replica set name (if any), any
   authentication credentials (e.g. username, password, database), and the
   process ID. If a connection already exists for that hash, it will be used in
   lieu of creating a new connection associated with that hash.
   <span class="function"><a href="mongoclient.getconnections.html" class="function">MongoClient::getConnections()</a></span> may be used to retrieve info
   about each persistent connection. Consider the following program:
  </p>

  <div class="example" id="mongo.writeconcerns.acknowledged-example">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$m1&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://localhost'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$m2&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://localhost'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$m3&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://user:pw@localhost'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$m4&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://127.0.0.1'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$m5&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://rs1.local:30017,rs2.local:30018/?replicaSet=rs'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$m6&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://sharding.local:40017'</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">::</span><span style="color: #0000BB">getConnections</span><span style="color: #007700">()&nbsp;as&nbsp;</span><span style="color: #0000BB">$conn</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$conn</span><span style="color: #007700">[</span><span style="color: #DD0000">'hash'</span><span style="color: #007700">],&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>The above example will output
something similar to:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
localhost:27017;-;X;15487
localhost:27017;-;admin/user/c56c…8bbc;15487
127.0.0.1:27017;-;X;15487
rs1.local:30017;rs;X;15487
rs2.local:30018;rs;X;15487
sharding.local:40017;-;X;15487
</pre></div>
   </div>
  </div>

  <p class="para">
   In this example <em>$m1</em> and <em>$m2</em> have the
   same hash and share a persistent connection. Connections for each other
   MongoClient instance hash to unique values and use their own sockets. Note
   that &quot;localhost&quot; and &quot;127.0.0.1&quot; do not share the same hash; DNS resolution
   is not taken into account.
  </p>
 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.connecting.uds.html">Domain Socket Support</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.connecting.pools.html">Connection Pooling (version 1.2.0-1.2.12 *only*)</a></div>
 <div class="up"><a href="mongo.connecting.html">Connecting</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>