NYCPHP Meetup

NYPHP.org

[nycphp-talk] When to close a mysql connection

David Krings ramons at gmx.net
Sat Jun 30 22:30:40 EDT 2007


Jakob Buchgraber wrote:
> No that's not how it works, that's how normal database connections work.
> How should MySQL know which user connects? The user makes one HTTP 
> Request and if he does it again nobody knows that he has ever been here 
> (except if the webserver has set a cookie). I don't know how persistent 
> connections exactly work as I haven't used them before. Everything I 
> know and probably everything you need to know from a developers point of 
> view is that when using persistent connections the connection is kept 
> open even if the execution of the PHP script finishes and if the script 
> is executed again and again there is always the same connection being 
> used. If two scripts access this connection concurrently there is no 
> second connection opened, no they are sharing this connection with each 
> other.
> 
> That's actually how I understood it. So correct me if I am wrong ... :-)


I am in no position to correct you, but isn't it that PHP talks to MySQL 
rather then the HTTP client? As far as MySQL is concerned, the 
connection comes from where the PHP interpreter is located. And since 
each connection from PHP to MySQL gets named sufficiently uniquely I 
guess that PHP can keep multiple clients apart. Normally one opens a 
connection, runs the queries, and then closes it. I always make sure 
that I explicitly close the connection, but after reading the 
documentation a script end will close the connection as well. Seems to 
be quite different from opening files, which stay open and locked even 
after the script is done.
It may just be that PHP uses only one persistent connection and feeds 
all the queries through that one regardless of client (but returns the 
values back to the script instance from where the query came), which is 
what you suspect. Unless someone who really knows what is going on comes 
forward we can only resort to speculation.

I will give it a try and see what happens. It's only adding a "p". Since 
the mysql_close doesn't do any thing on persistent connections there is 
no harm in leaving it in - or so the theory goes.

I don't see a problem with my current project as this one will never be 
used by more than a few dozen clients at a time, but I have other 
projects where some optimization may be more than just OK.


David



More information about the talk mailing list