Forum Moderators: coopster

Message Too Old, No Replies

How to close MySQL connections open with mysql pconnect?

I suffer a high resource waste with mySQL

         

guarriman

3:48 pm on Dec 4, 2007 (gmt 0)

10+ Year Member



Hi.

Using PHP 4, I open MySQL connections with:
---
$database = mysql_pconnect($db_host, $db_user, $db_pass);
---

I don't close this connection because official documentantion (http://es.php.net/mysql_pconnect) claims that "mysql_close() will not close links established by mysql_pconnec".

How can I close these connections? Thank you very much.

eelixduppy

3:55 pm on Dec 4, 2007 (gmt 0)



The whole idea behind a persistent connection [php.net] is that it doesn't close once it is open.

lammert

1:48 am on Dec 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Persistent connections are efficient because there is no overhead to make a connection between PHP and MySQL, every time a PHP script is executed. The main drawback however is that resources may be easily wasted because MySQL keeps some data of that connection in its cache, even when no PHP script is currently using the connection.

A common problem are result sets of large SELECT queries that may stay in memory long after there usefulness in the PHP script that requested the data has ended. These result sets are using available resources and could be the source of the resource problem you currently see.

The proper way to deal with this is carefully calling the mysql_free_result() function whenever returned data from a SELECT statement is no longer needed. This may free up enough resources for your MySQL server to run smoothly again.

Furthermore the behaviour of persistent connections can be tuned in your PHP.INI file with the mysql.allow_persistent and mysql.max_persistent settings.

coopster

3:26 pm on Dec 7, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Also remember that HTTP is a stateless protocol so persistence is only good for a single request instance. In other words, you won't likely need to use persistent connections in most, if not all, of your work.

A few resources in regards to persistent connections:
[php.net...]
[dev.mysql.com...]