Parsing the difference between mysql_connect and Mysql_pconncet in PHP _php tutorial

Source: Internet
Author: User
Tags pconnect
Talk about the difference between mysql_connect and Mysql_pconnect, the two functions of the use of the same, the internet has said should be used Pconnect, pconnect is a good thing, also see pconnect as a scourge, resolutely not to use pconnect, Also have an ambiguous attitude. And what about this thing?

Permanent link does not mean that the server has a connection open, and then all the people share the link. Permanent connection is the same as each client to open a connection, there are 200 people access to have 200 connections. In fact, Mysql_pconnect () itself did not do too much processing, the only thing it does is to not actively close the MySQL connection after the end of the PHP run.

Pconnect and connect are basically no different when PHP is running CGI, because the CGI approach is a process that each PHP accesses, and the process ends after the visit, and the resources are all released. When PHP is running in the Apache module, because Apache has a process pool, a httpd process will be put back to the process pool, which makes the MySQL connection resource opened with Pconnect not be released. Then there is the next connection request that can be reused. This makes it possible to get access faster when Apache has a small number of concurrent accesses, and because of the use of Pconnect, PHP saves time repeatedly connecting to the DB. This should be better understood. However, when the Apache concurrent access is large, if you use Pconnect, because the previous httpd process occupied by the MySQL connection is not close, it may be because MySQL has reached the maximum connection, So that some of the subsequent requests will never be satisfied. If the maximum number of MySQL connections is set to 500, while Apache's maximum number of simultaneous accesses is set to 2000, assuming that all access will require access to the DB, and the operation time will be longer, the current 500 requests httpd is not the end of the time, Subsequent HTTD processes are unable to connect to MySQL (due to the maximum number of MySQL connections already reached). MySQL can be connected only if the current 500 httpd processes are terminated or reused.

When the DB operation is complex and takes a long time, the httpd will fork many concurrent processes, and the resulting httpd process does not release the DB connection, making the resulting HTTPD process unable to connect to the DB. Because this does not re-use the MySQL connection of the other httpd process. So there will be a lot of connection timeouts. When concurrent traffic is not high, the use of pconnect can simply improve access speed, but after the increase in concurrency, whether or not to use pconnect will see the programmer's choice.

In my opinion, PHP is now connected to MySQL does not really use the connection pool, Pconnect is only equivalent to borrow the Apache process pools, so when the concurrent traffic is large pconnect does not improve the efficiency of access to the DB.

In the actual application, with mysql_pconnect words, each refresh and request new pages are relatively fast, and with mysql_connect words, each refresh to re-request, when the database connection is relatively slow, you can see the difference. When your database connection is slow, the DB operation is not very complicated, and your program is confident enough not to generate a deadlock, or you have control over the server to meet any of the above four conditions of any two, then you can use a pconnect.

Pconnect does not have to be closed in the script, you can set lifetime in MySQL, or you can write the shell to scan regularly, kill off the long-dormant connection. one sentence summary: To use good pconnect, not only the PHP script thing also related to the database and server settings.

http://www.bkjia.com/PHPjc/327140.html www.bkjia.com true http://www.bkjia.com/PHPjc/327140.html techarticle talk about the difference between mysql_connect and Mysql_pconnect, the use of these two functions, the internet has said should be used Pconnect, pconnect is a good thing, also see pconnect as a scourge, ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.