Pconnect or connect

mysql_pconnect on shared hosting- is this still the “encouraged” method on DH? Works fine? Or is connect better?


I don’t know what DH’s preferred method is, but I would recommend you just use connect(). pconnect() is a deceving function. Here’s a good note on it from the php manual comments:

"Normally you do NOT want to use mysql_pconnect. This function is designed for environments which have a high overhead to connecting to the database. In a typical MySQL / Apache / PHP environment, Apache will create many child processes which lie in idle waiting for a web request to be assigned to them. Each of these child processes will open and hold its own MySQL connection. So if you have a MySQL server which has a limit of 50 connections, but Apache keeps more than 50 child processes running, each of these child processes can hold a connection to your MySQL server, even while they are idle (idle httpd child processes don’t lend their MySQL connection to other httpd children, they hold their own). So even if you only have a few pages which actually connect to MySQL on a busy site, you can run out of connections, with all of them not actually being used.

In general use mysql_connect() for connecting to MySQL unless that connection takes a long time to establish."

You probably won’t notice any difference in performance between the two types of connections.

Thank you for the informative reply. I’ve been leery of using pconnect, but was about to try- if it would have made me a better neighbor on the machine. Sounds like it would do the opposite. Thanks again!


I think that it depends on the situation, connections take processing. Use the example of a busy bulletin board and someone surfing many many pages over the course of a long period of time, in this case it would be better to be using a persistent connection than to reconnect every page turn.

I think you can take the old system of tallying “conueries” here at dreamhost to mean that connections have an associated cost. Just because they’ve dropped the tally/charging for overage doesn’t mean you shouldn’t still try to be responsible, which it sounds like you are :slight_smile:



Ok, thinking and rethinking this over… So for a not-overly-busy membership site that only requires the database info on one or two pages per person, connect ought to be fine? Thank you, jason, for your thoughts.


If a site is very popular, pconnect() can cause issues as well, depending on the host server configuration. Specifically, running out of available MySQL connections, and preventing others from accessing the database server. With pconnect() each apache process that serves your site will open it’s own persistent connection using those parameters and keep that resource until it dies, unless the host server has a resonable wait_timeout MySQL value.

mysql_pconnect won’t even make a difference if you’re using the CGI version of PHP (which is the default.) The CGI will drop all connections when the script is done executing, pconnect or not.

Thanks for all the input here. Oh yah! That whole cgi thing- I remember something being mentioned about that somewhere. Big help that you brought it up here, and it saved a lot of hassle.