closing db connections

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

closing db connections

Post by microthick »

do u consciously close your db connection every time you're done with it?

what happens if i just continue on to another page without closing the connection... is this bad practice?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

I always close it. Though I did use an open source script where it wasn't closed in a hundred or so places but it didn't make for any security leak or MySQL errors. Just bad practice. I recommend doing so, especially if you're going to release the application to the world.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

If connection isn't persistent it will be closed during PHP shudown sequence.
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post by fractalvibes »

Always good practice to close all connections, dispose of objects,etc.
after you are done with them. Esp. with anything where licensing gives you N number of concurrent instances and no more!
Some languages might have good garbage collection whilst others do not.
Run a tight ship in that respect and you'll be better off.

FV
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Sami wrote:I always close it. Though I did use an open source script where it wasn't closed in a hundred or so places but it didn't make for any security leak or MySQL errors. Just bad practice. I recommend doing so, especially if you're going to release the application to the world.
I'd recommend to close it too.
Quoting php.net: "...all resources that were in use for this resource are made free by the garbage collector. For this reason, it is rarely ever necessary to free the memory manually by using some free_result function."

With my limited knowledge of english and how the Zend engine/garbage collector works, 'rarely' is not enough for me.
Post Reply