Parsing the differences between mysql_connect and mysql_pconncet in php _ php instance
Source: Internet
Author: User
This article analyzes the differences between mysql_connect and mysql_pconncet in php. For more information, see the differences between mysql_connect and mysql_pconnect. The usage of these two functions is similar, pconnect should be used on the Internet. pconnect is a good thing. pconnect is also regarded as a flood of animals, so it is hard to prevent pconnect from being used. What is this?
The permanent link does not mean that the server opens a connection and all people share the link. A permanent connection means that each client opens a connection, and 200 connections can be accessed by 200 people. Its actual mysql_pconnect () does not handle much. The only thing it does is that it does not actively close the mysql connection after the php operation ends.
There is basically no difference between pconnect and connect when php runs in cgi Mode, because each php accesses a process and the process ends after the access is complete, all resources are released. when php runs as an apache module, because apache uses a process pool, an httpd process will be put back into the process pool after it ends, this will prevent the mysql connection resource opened with pconnect from being released, so it can be reused when there is a connection request. this makes it possible for php to save time for repeatedly connecting to the database due to the use of pconnect when the concurrency traffic of apache is low, which speeds up access. this should be easy to understand. however, if you use pconnect when the number of concurrent accesses to apache is large, it may be because the mysql connection occupied by some httpd processes has not been closed, in this way, subsequent requests will never be satisfied. if the maximum number of connections in mysql is set to 500, and the maximum number of concurrent connections in apache is set to 2000, it is assumed that all accesses require access to the database, and the operation time will be relatively long, when the httpd of the current 500 requests are not completed, the subsequent httd processes cannot connect to mysql (because the maximum number of connections in mysql has been reached ). mysql can be connected only when the current 500 httpd processes are terminated or reused.
When db operations are complex and take a long time, because httpd will fork many concurrent processes for processing, the first httpd process will not release the db connection, making the subsequent httpd process unable to connect to the db. as a result, the mysql connection of other httpd processes is not reused. therefore, many connection times out. When the concurrency traffic is not high, pconnect can be used to increase the access speed. However, when the concurrency increases, whether to use pconnect again depends on the programmer's choice.
I personally think that the connection pool is not actually used by php for mysql connections, and pconnect is only used by the apache process pool, therefore, pconnect does not greatly improve the efficiency of accessing the database when the concurrency traffic is high.
In actual applications, if mysql_pconnect is used, each refresh and request for a new page are relatively fast. If mysql_connect is used, each refresh request must be made again. When the database connection is slow, we can see the difference. When your database connection is slow, DB operations are not very complex, and your program is confident enough to avoid deadlocks, or you have control over the server, if any two of the above four conditions are met, you can use pconnect.
Pconnect does not need to be closed in the script. You can set the life time in mysql, Or you can write a shell to scan regularly to kill connections with too long sleep. In a word, pconnect should be used well, not only for php scripts, but also for database and server settings.
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