When should you close the database connection?
When I learned PHP/MySQL from a friend he always closed the database connection after every query he made, but now I have read "Professional PHP5" and it says that opening a database connection takes more resources then all but the most complex queries.
I have built a database abstraction layer with the code from chapter 8 but changed from PostgreSQL to MySQL. I didn't use anything from the PEAR DB section either, except I added some function for selecting one row and I also added the singelton design pattern.
So now when I use my class I open a connection get all the stuff I need from the database and when the page is loaded the destructor closes the connection. But then I heard from other sources that leaving a connection open that long wont be good if you have a lot of visitors. Whats worse having connections open while you load the whole page or opening and closing a connection almost after every query? And is there sometime when one or the other is better?
|