leesiulung Posted May 4, 2007 Share Posted May 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
veridicus Posted May 4, 2007 Share Posted May 4, 2007 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. Quote Link to comment Share on other sites More sharing options...
leesiulung Posted May 4, 2007 Author Share Posted May 4, 2007 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.... Quote Link to comment Share on other sites More sharing options...
veridicus Posted May 7, 2007 Share Posted May 7, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.