What is the difference between MySQL_connect and MySQL_pconnect?

Source: Internet
Author: User
Tags pconnect

The following articles mainly introduce the differences between MySQL_connect and MySQL_pconnect in php. I saw the differences between MySQL_connect and MySQL_pconnect in php two days ago, I have provided it for you to share today.

I was asked about the difference between MySQL_connect and MySQL_pconnect when I went to the interview a while ago. Unfortunately, I only answered one question and it seems that I have not been recognized yet ~

I reviewed the php manual record.

In short, MySQL_pconnect is used to establish a persistent connection between php and MySQL. Generally, the php Execution Mode is to initialize all resources when the script starts to be executed, and release all resources after the script ends. this is not the case with MySQL_pconnect. Every time MySQL_connect re-establishes a relationship with the SQL server through tcp or unix domian socket, each handshake consumes a lot of server resources.

When pconnect is used and a request is sent to connect to MySQL, php checks whether there is an identical connection (connected to the same MySQL server with the same user name and password, if yes, you can directly use this connection. It is worth noting that the same connection concept is for processes. Multiple connections will be established when different processes call MySQL_pconnect.

Connect and pconnect do not bring about functional differences, but only performance differences.

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, which is also answered by my brother.

Assume that the MySQL server is configured to support a maximum of 10 concurrencies, and apache is configured to use 100 sub-processes.

Apache is coordinated by a parent process to distribute the received http request to the idle sub-process for processing. As a result, it quickly processes 10 http requests, if 10 sub-processes are allocated to different sub-processes, the persistent connections between the 10 sub-processes and MySQL are established, and the MySQL capability has reached the limit.

At this time, an http request is sent. apache assigns it to any other process that is not in the 10 sub-processes. Then, there is no way for this process to establish a connection to MySQL, because the slot is full.

Other problems may occur when using persistent connections.

If a persistent connection is used in your script and the table Lock operation is performed, if the script is not unlocked by the end of the script. if you run this script again next time, it will wait for its unlock table to get the lock table, and it will no longer be able to come back in the past, which is an endless loop. unless you restart the web or MySQL server. the other cause is the transaction.

To avoid this problem, you can use register_shutdown_function to register a callback function. In this case, release the table lock or roll back the transaction.

The above content is an introduction to the differences between MySQL_connect and MySQL_pconnect in php. I hope you will find some gains.

Related Article

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.