Forum Moderators: coopster
According to the mysql documentation, if you use persistent connections there should only ever be one open at any given time for any given user. Plus you don't need to close your connections with mysql_close(); However after trying this simultaneously on only a few sites on one server I soon ran out of available connections and started getting errors. By telnetting in and using the SHOW PROCESSLIST command I could see that despite using persistent connections there were up to 15 connections open per user.
So I went through about 12 of my sites and changed all my database connection scripts to use the standard mysql_connect. Guess what, it seems to be worse - more simultaneous connections than before. Yet many believe this is the best option for shared servers.
I was just wondering if anyone had experience of this and had come to a conclusion about whish is best to use on a shared server.
mysql_pconnect establishes a persistent connection. If you don't need one (such as a website that is mostly HTML files or PHP files that don't call the db) then you don't need to use it.
mysql_connect establishes a connection for the duration of the script that access the db. Once the script has finished executing it closes the connection. The only time you need to close the connection manually is if you jump out of the script for any reason.
If you do use mysql_pconnect. You only need to call it once for the session. That's the beauty of it. It will hold open a connection to the db that you can use over and over again simply by calling the resource ID whenever you need to interact with the db.
Does this help?
So am I correct in thinking that if you have already used mysql_pconnect during the session and you call it again later no more connections should be opened, because mysql checks to see if there is one already open for that user? If so I think there may be some config tweaking to do on my server because that isn't the way it's working at all. Are you saying that you could have 3 visitors online, each generating a unique session and therefore each having their own persistant connection? The only guidance php.net [php.net] gives on this matter is,
"Using persistent connections can require a bit of tuning of your Apache and MySQL configurations to ensure that you do not exceed the number of connections allowed by MySQL."
which isn't particularly helpful.
Do you know how long persistent connections last. I have read it's up to 8 hours which might explain why I was exceeding my limit. Although I thought that if it was working correctly each of my sites should only have one connection open at any given time.
You don't need to call mysql_pconnect again once you've called it. In other words. Call it once like so:
$connect = mysql_pconnect( "yourdbname", "yourusername", "yourpassword")
This will establish a resourceID ($connect) which you save as a session var. Then the next time you need to interact with the db you'd write the db select statement as mysql_select_db("dbname", $connect);
Not sure that's the most efficient way to do it but I'm sure someone will correct me if it isn't.
usage of pconnect/connect IS exactly the same
it's different for it's internal implement
not scripting, so.. no saving resourceID, no session
mysql_pconnect will keep the connecton but not the mysql-user-login
every time the script start, u have to do mysql_pconnect, to login user