PHP pconnect and Singleton mode to connect to the database, how to be the most high-performance connection database?

Source: Internet
Author: User
Tags connection pooling pconnect
The following three ways to connect
1. Create a singleton class with the mysql_connect () function
2. Establish a long connection directly using the Mysql_pconnect ()
3. Establish a database connection class, instantiate the time to establish a short connection, when the destruction is closed (mysql_close)
Can you tell me which of the above three types of applications have the highest performance to connect to a database?
(ps:php have the need to implement connection pooling)

Reply content:

= = Are you a PPC person? = = The cabbage has been replied ... I'll forward it ...


At the PHP level, no matter how you write code, it's Dora, no difference. Let me emphasize the difference between a long connection and a normal link:
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). Only the current 500 httpd processes are terminated or reused to connect to MySQL
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.
PHP connection 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 you have enough confidence in your own program, no deadlock, and you have control over the server, you can meet any of the above four conditions of any two, then use 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. In a nutshell: To use the good pconnect, not only the PHP script thing also related to the database and server settings.

Original address: / http Bbs.phpchina.com/thread -238077-1-1.html
  • 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.