Forum Moderators: coopster

Message Too Old, No Replies

PHP/MySql connection open/close checker?

Is there such a thing?

         

HughMungus

9:18 pm on Sep 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there a tool that will check my code to make sure that my PHP is closing all the db connections that it's opening?

nalin

9:29 pm on Sep 17, 2004 (gmt 0)

10+ Year Member



Not your code per say, but...
dependant upon the host architecture you can view open connections after executing the php script from a command line for instance with linux "netstat" will list open connections.

Incedentally php will not necessarily close connections (dependant upon persistant flag it will leave them open for future runs or until they timeout)

HughMungus

9:35 pm on Sep 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, but this is shared hosting and I don't have shell access. I was hoping for something that could look at my code and tell me if I'm opening connections and not closing them (depending on the name of the connection each time I open one).

mincklerstraat

9:40 pm on Sep 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you're on shared hosting I'd guess you're not using persistent connections. Normally in default circumstances on shared hosting php will close your mysql connections after the page has been executed. See [be2.php.net...] the 'usually not necessary' part. Is there anything indicating to you that your db connections might not be closing?

HughMungus

9:47 pm on Sep 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I got a notice from my host that I had too many concurrent connections open. They said I had 72,000 at once so I thought that maybe I wasn't closing connections properly. Honesly, I think they're trying to cover for bad hosting service (this wouldn't be the first time I've had problems with them). Ugh.

mincklerstraat

10:13 pm on Sep 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wow, that's a lot of concurrent connections. I'm by no means a server expert - above comes from what I read in the PHP manual and not from experience.

I've never used persistent connections myself, but you could check your script at the beginning to see if it's using mysql_connect or mysql_pconnect - the latter which doesn't close connections. The manual notes that using mysql_pconnect can require some special tuning of apache and mysql - for the reason that you can get too many concurrent connections - probably for this reason most scripts on shared hosting don't use it.

nalin

6:43 pm on Sep 18, 2004 (gmt 0)

10+ Year Member



Hypothetically you could

Mirror mysql database on a second machine
Change machine reference in mysql_connect
Run netstat on the second machine

72,000 is huge - dont know a great deal about the setup of shared hosts but the default mysql variable for concurrent connections is something like ~100 (64 mabye). Additionally - assuming 1 connection per pageview (I cannot think of an application that would require more than this), I am sure it represents a good amount of time as well. Even if the connections are set up persistantly you should not see this - I would double check that you close the connection and add perhaps an "or die('could not close the connection');" or similar to the end of mysql_close statement.

[edit]

tell me if I'm opening connections and not closing them (depending on the name of the connection each time I open one)

Just to clarify here - generally one should open a connection once near the top off the code and close it once near the bottom (for each database used). The overhead and time required for opening and closing is large (think stoping and starting a car vs leaving it running for 30 sec). Unless there is some specific reason to use multiple connections - for instance privledge differences of some sort - then you should use only one and in any case are better of handling all openings in one location and all closings in one location

hiker_jjw

7:22 pm on Sep 18, 2004 (gmt 0)



I typically use phpMyAdmin for all of my MySQL needs. If you have root access you can see the processes running, kill them, etc. Just my way of doing things.