features.persistent-connections.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 151 行

HTML
151
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Persistent Database Connections</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="features.connection-handling.html">Connection handling</a></div> <div class="next" style="text-align: right; float: right;"><a href="features.safe-mode.html">Safe Mode</a></div> <div class="up"><a href="features.html">Features</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div>  <h1>Persistent Database Connections</h1>  <p class="simpara">   Persistent connections are links that do not close when the   execution of your script ends. When a persistent connection is   requested, PHP checks if there&#039;s already an identical persistent   connection (that remained open from earlier) - and if it exists, it   uses it. If it does not exist, it creates the link. An &#039;identical&#039;   connection is a connection that was opened to the same host, with   the same username and the same password (where applicable).  </p>  <p class="simpara">   People who aren&#039;t thoroughly familiar with the way web servers work   and distribute the load may mistake persistent connects for what   they&#039;re not. In particular, they do <em class="emphasis">not</em> give   you an ability to open &#039;user sessions&#039; on the same link, they   do <em class="emphasis">not</em> give you an ability to build up a   transaction efficiently, and they don&#039;t do a whole lot of other   things. In fact, to be extremely clear about the subject,   persistent connections don&#039;t give you <em class="emphasis">any</em>   functionality that wasn&#039;t possible with their non-persistent   brothers.  </p>  <p class="simpara">   Why?  </p>   <p class="simpara">   This has to do with the way web servers work. There are three ways   in which your web server can utilize PHP to generate web pages.  </p>  <p class="simpara">   The first method is to use PHP as a CGI &quot;wrapper&quot;. When run this   way, an instance of the PHP interpreter is created and destroyed   for every page request (for a PHP page) to your web server.   Because it is destroyed after every request, any resources that it   acquires (such as a link to an SQL database server) are closed when   it is destroyed. In this case, you do not gain anything from trying   to use persistent connections -- they simply don&#039;t persist.  </p>  <p class="simpara">   The second, and most popular, method is to run PHP as a module in a   multiprocess web server, which currently only includes Apache. A   multiprocess server typically has one process (the parent) which   coordinates a set of processes (its children) who actually do the   work of serving up web pages. When a request comes in from a   client, it is handed off to one of the children that is not already   serving another client. This means that when the same client makes   a second request to the server, it may be served by a different   child process than the first time. When opening a persistent connection,    every following page requesting SQL services can reuse the same    established connection to the SQL server.  </p>  <p class="simpara">   The last method is to use PHP as a plug-in for a multithreaded web   server. Currently PHP 4 has support for ISAPI, WSAPI, and NSAPI (on   Windows), which all allow PHP to be used as a plug-in on multithreaded   servers like Netscape FastTrack (iPlanet), Microsoft&#039;s Internet Information   Server (IIS), and O&#039;Reilly&#039;s WebSite Pro. The behavior is essentially   the same as for the multiprocess model described before.  </p>  <p class="simpara">   If persistent connections don&#039;t have any added functionality, what   are they good for?  </p>  <p class="simpara">   The answer here is extremely simple -- efficiency. Persistent   connections are good if the overhead to create a link to your SQL   server is high. Whether or not this overhead is really high depends   on many factors. Like, what kind of database it is, whether or not   it sits on the same computer on which your web server sits, how   loaded the machine the SQL server sits on is and so forth. The   bottom line is that if that connection overhead is high, persistent   connections help you considerably. They cause the child process to   simply connect only once for its entire lifespan, instead of every   time it processes a page that requires connecting to the SQL   server. This means that for every child that opened a persistent   connection will have its own open persistent connection to the   server. For example, if you had 20 different child processes that   ran a script that made a persistent connection to your SQL server,   you&#039;d have 20 different connections to the SQL server, one from   each child.  </p>  <p class="simpara">   Note, however, that this can have some drawbacks if you are using a   database with connection limits that are exceeded by persistent   child connections. If your database has a limit of 16 simultaneous   connections, and in the course of a busy server session, 17 child   threads attempt to connect, one will not be able to. If there are   bugs in your scripts which do not allow the connections to shut   down (such as infinite loops), the database with only 16 connections   may be rapidly swamped. Check your database documentation for   information on handling abandoned or idle connections.  </p>  <div class="warning"><b class="warning">Warning</b>   <p class="simpara">    There are a couple of additional caveats to keep in mind when    using persistent connections. One is that when using table    locking on a persistent connection, if the script for whatever    reason cannot release the lock, then subsequent scripts using the    same connection will block indefinitely and may require that you    either restart the httpd server or the database server. Another is    that when using transactions, a transaction block will also carry    over to the next script which uses that connection if script execution    ends before the transaction block does. In either case, you can    use <a href="function.register-shutdown-function.html" class="function">register_shutdown_function()</a> to register a    simple cleanup function to unlock your tables or roll back your    transactions. Better yet, avoid the problem entirely by not using    persistent connections in scripts which use table locks or    transactions (you can still use them elsewhere).   </p>  </div>  <p class="simpara">   An important summary. Persistent connections were designed to have   one-to-one mapping to regular connections. That means that you   should <em class="emphasis">always</em> be able to replace persistent   connections with non-persistent connections, and it won&#039;t change   the way your script behaves. It <em class="emphasis">may</em> (and   probably will) change the efficiency of the script, but not its   behavior!  </p>  <p class="para">   See also <a href="function.fbsql-pconnect.html" class="function">fbsql_pconnect()</a>,   <a href="function.ibase-pconnect.html" class="function">ibase_pconnect()</a>, <a href="function.ifx-pconnect.html" class="function">ifx_pconnect()</a>,   <a href="function.ingres-pconnect.html" class="function">ingres_pconnect()</a>,   <a href="function.msql-pconnect.html" class="function">msql_pconnect()</a>, <a href="function.mssql-pconnect.html" class="function">mssql_pconnect()</a>,   <a href="function.mysql-pconnect.html" class="function">mysql_pconnect()</a>, <a href="function.ociplogon.html" class="function">ociplogon()</a>,   <a href="function.odbc-pconnect.html" class="function">odbc_pconnect()</a>, <a href="function.oci-pconnect.html" class="function">oci_pconnect()</a>,   <a href="function.pfsockopen.html" class="function">pfsockopen()</a>, <a href="function.pg-pconnect.html" class="function">pg_pconnect()</a>, and   <a href="function.sybase-pconnect.html" class="function">sybase_pconnect()</a>.  </p> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="features.connection-handling.html">Connection handling</a></div> <div class="next" style="text-align: right; float: right;"><a href="features.safe-mode.html">Safe Mode</a></div> <div class="up"><a href="features.html">Features</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?