Jump to content

persistent connection behavior: mysql_pconnect() and mysql_close()


leesiulung

Recommended Posts

Reading the online manual about mysql_pconnect() and mysql_close() I have a problem understanding how one can close a persistent connection? Do one even need to?

 

Do most people use mysql_connect() or mysql_pconnect (persistent)?

 

The environment is just shared hosting with GoDaddy....

 

Also, the anomality with the reference counter and close() is very bad. More info mentioned in the comments of mysql_close() documentation.

 

 

mysql_close does not actually close a connection opened with pconnect.  When you're on a single-server environment it's often benefitial to not close the connection when the script is done so it can remain open for the next request.  Opening a connection used to be more expensive, but now it's very quick even with connect.  On a shared host where you most likely connect to a database on a different server it's better to not use pconnect.  If everyone on the network sharing the database used pconnect the database server can run out of connections.

 

So my suggestion is to use mysql_connect and optionally mysql_close.  I believe the connection is automatically closed when the script quits anyway.

thanks verdicius.

 

However, I'm still not clear on what happens with a persistent connection. At what point does it free its resources?

 

It would have been better if they had the database server manage the connection pool instead....

Well, the database server does manage the connection pool... sorta.  The connection is closed when the PHP instance closes or the database says it timed out.  Since apache limits the number of simultaneously running PHP instances, and MySQL limits the number of connections and timeouts, you know the max number of connections your servers will handle.

 

Of course with multi-server setups it's often best to use load balancers, which then take care of the connection pool instead.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.