Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 9683

php-manual-en-5.5.7-1.mga4.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>The mysqli Extension and Persistent Connections</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysqli.resources.html">Resource Types</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqli.constants.html">Predefined Constants</a></div>
 <div class="up"><a href="book.mysqli.html">Mysqli</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mysqli.persistconns" class="chapter">

 <h1>The mysqli Extension and Persistent Connections</h1>


 <p class="para">
  Persistent connection support was introduced in PHP 5.3 for the
  <em>mysqli</em> extension. Support was already present in
  PDO MYSQL and ext/mysql. The idea behind persistent connections is
  that a connection between a client process and a database can be
  reused by a client process, rather than being created and destroyed
  multiple times. This reduces the overhead of creating fresh
  connections every time one is required, as unused connections are
  cached and ready to be reused.
 </p>
 
 <p class="para">
  Unlike the mysql extension, mysqli does not provide a separate function
  for opening persistent connections. To open a persistent connection you
  must prepend <em>p:</em> to the hostname when connecting.
 </p>

 <p class="para">
  The problem with persistent connections is that they can be left in
  unpredictable states by clients. For example, a table lock might be
  activated before a client terminates unexpectedly. A new client
  process reusing this persistent connection will get the connection
  "<span class="quote">as is</span>". Any cleanup would need to be done by the new
  client process before it could make good use of the persistent
  connection, increasing the burden on the programmer.
 </p>

 <p class="para">
  The persistent connection of the <em>mysqli</em> extension
  however provides built-in cleanup handling code. The cleanup carried
  out by <em>mysqli</em> includes:
 </p>

 <ul class="itemizedlist">

  <li class="listitem">
   <p class="para">
    Rollback active transactions
   </p>
  </li>

  <li class="listitem">
   <p class="para">
    Close and drop temporary tables
   </p>
  </li>

  <li class="listitem">
   <p class="para">
    Unlock tables
   </p>
  </li>

  <li class="listitem">
   <p class="para">
    Reset session variables
   </p>
  </li>

  <li class="listitem">
   <p class="para">
    Close prepared statements (always happens with PHP)
   </p>
  </li>

  <li class="listitem">
   <p class="para">
    Close handler
   </p>
  </li>

  <li class="listitem">
   <p class="para">
    Release locks acquired with  <span class="function"><strong>GET_LOCK()</strong></span>
   </p>
  </li>

 </ul>

 <p class="para">
  This ensures that persistent connections are in a clean state on
  return from the connection pool, before the client process uses them.
 </p>

 <p class="para">
  The <em>mysqli</em> extension does this cleanup by
  automatically calling the C-API function
  <em>mysql_change_user()</em>.
 </p>

 <p class="para">
  The automatic cleanup feature has advantages and disadvantages though.
  The advantage is that the programmer no longer needs to worry about
  adding cleanup code, as it is called automatically. However, the
  disadvantage is that the code could <em class="emphasis">potentially</em>
  be a little slower, as the code to perform the cleanup needs to run
  each time a connection is returned from the connection pool.
 </p>

 <p class="para">
  It is possible to switch off the automatic cleanup code, by compiling
  PHP with

  <strong><code>MYSQLI_NO_CHANGE_USER_ON_PCONNECT</code></strong>

  defined.
 </p>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="para">
   The <em>mysqli</em> extension supports persistent
   connections when using either MySQL Native Driver or MySQL Client
   Library.
  </p>
 </p></blockquote>

</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysqli.resources.html">Resource Types</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqli.constants.html">Predefined Constants</a></div>
 <div class="up"><a href="book.mysqli.html">Mysqli</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>