Forum Moderators: coopster

Message Too Old, No Replies

mysql_pconnect vs mysql_connect - the difinitive answer?

What's really the score?

         

rycrostud

6:55 pm on Jan 20, 2003 (gmt 0)

10+ Year Member



OK this is driving me a bit mad! There seem to be conflicting opinions on which is the best method of connecting to your mysql database.

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.

lorax

7:02 pm on Jan 20, 2003 (gmt 0)

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



rycrostud,

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?

rycrostud

7:46 pm on Jan 20, 2003 (gmt 0)

10+ Year Member



That sounds sensible!

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.

lorax

8:24 pm on Jan 20, 2003 (gmt 0)

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



I believe the duration of the timeout can be set by you or your host.

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.

c3oc3o

11:08 pm on Jan 20, 2003 (gmt 0)

10+ Year Member



Rycro, have you read this page at php.net?
[php.net...]

Xuefer

2:12 am on Jan 21, 2003 (gmt 0)

10+ Year Member



to lorax:
did u tried it?
there's no way to save resource
cos there's no "resource_still_exists()"

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

lorax

3:54 am on Jan 21, 2003 (gmt 0)

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



Xuefer,
No I didn't - but should have as you are correct. I was shooting from the hip and shouldn't have. I use a variety of functions from a standard file to make such connections and confess I haven't looked at that file in quite a while. Vague memories have gotten me in trouble before - you'd think I'd learn. ;) My sincere apologies.

rycrostud

1:36 pm on Jan 21, 2003 (gmt 0)

10+ Year Member



Thanks c3oc3o!

That article is excellent. It explains my server behaviour perfectly and I recommend that everyone reads it.

It seems that if you have a multi-process web server then there may be little benefit in using persistent connections.