Differences between mysql_connect and mysql_pconnect

Source: Internet
Author: User
Tags pconnect

Generally, PHP runs in two modes: CGI and Apache. as a CGI, connect is no different from pconnect, because every time CGI is run, resources are destroyed and cleared.

PHP can be used for continuous database connection when running as an Apache module, but there may be potential problems


Apache module mode:
The difference is that 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 the use of pconnect when the Apache concurrency traffic is low, PHP saves the time for repeatedly connecting to the database, and speeds up access. This should be a better understanding.

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.

For example:
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
Assume that all accesses require access to the database, and the operation takes a long time.

When the httpd of the current 500 requests is not completed... subsequent HTTD processes cannot connect to MySQL (because the maximum number of connections to MySQL has been reached ). mySQL can be connected only when the current 500 httpd processes are terminated or reused.

In fact, this is also a good explanation of the xgy_p test if the operation is relatively simple, pconnect is much more efficient than connect, and is close to the speed of using the JSP connection pool. at this time, the HTTPd process can be reused continuously.

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,
In this way, the subsequent HTTPd process cannot be connected to the DB. Because the MySQL connection of other httpd processes is not reused, many connection times out,
For example, in the first 1000 concurrent connection tests, almost all connection times out.

---
(In reverse view, If JSP uses a pure dB connection pool, there will be no connection failure due to reaching the MySQL connection ceiling, the JSP connection pool makes it possible to wait for other connections to be used up and reused .)

Therefore, when the concurrent access volume is not high, pconnect can be used to increase the access speed. However, when the concurrent access volume increases, whether to use pconnect again depends on the programmer's choice ..

 
Currently, PHP does not actually use the connection pool for MySQL connections. pconnect is equivalent to using the Apache process pool, therefore, pconnect does not greatly improve the efficiency of accessing the database when the concurrency traffic is high. at this point. PHP is indeed inferior to JSP

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.