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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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....

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.