Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing a Mysql Database connection: is it neccessary?

Status
Not open for further replies.

monkey64

Technical User
Apr 27, 2008
69
GB
I'm working on an Ajax live search search facility which uses Mysql Queries. The performance was sluggish, and I found the answer was to close the database connection at the end of the script using the mysql_close() function.

I'm sure it's good practice to close the db connection, but the books I have say it isn't neccessary. What do you think?
 
Hi

What kind of database connections, persistent or non-persistent ? ( Were they opened with [tt]mysql_connect()[/tt] or with [tt]mysql_pconnect()[/tt] ? )

Non-persistent connections are automatically closed when the garbage collector frees up the resource. So explicitly closing a non-persistent connection usually has no effect.

Persistent connections are not automatically closed and that is the point : when serving subsequent requests, no new connection is established, but the previous reused. ( Supposing that PHP runs as web server module. ) So explicitly closing a persistent connection harms by voiding its advantage over non-persistent connections.

So far we know that you are using PHP and MySQL. If you specify their versions and what web server and operating system are you using, someone may have some ideas/advices. Of course, if you post your code someone may discover some inefficiencies.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top