Posted on 3 Comments

Persistent connections in PHP

mysql_pconnect() in PHP… you don’t want it. Really.

If you use Apache 1.3, or 2.0 with PreFork, there will be an instance of PHP inside each Apache process. Each of these will have its own cache of persistent connections.

MySQL actually has a very fast connection setup, so it doesn’t save time.
With persistent connections, PHP needs to do some cleanup of a connection, to make sure there are no in-progress transactions, or changed server variables. This is a somewhat inexact science. So much so that Georg Richter has actually completely disabled persistent connections in the PHP 5 mysqli extension.

To disable persistent connections, just use mysql_connect() instead.
You can also, very simply, set mysql.allow_persistent = Off in your php.ini file.

Posted on 3 Comments

3 thoughts on “Persistent connections in PHP

  1. if you really performance-savvy, you’d have to enable thread_cache_size, so that MySQL would pool free threads for next incoming connections. This way _pconnect() is so dead!

  2. Indeed! Thanks, I forgot to note that in my post in the end 😉

  3. User referenced to your post from Error handling for MySQL applications saying: […] the MySQL server end and some other things to look at on the application server end… I posted a note about the PHP issue and solution 4 years back.If the error code is 1040 (Too many connections)the situation is similar but you were … […]

Comments are closed.