Page 1 of 1
Forgot to close Recordsets at each page, Wht to do now
Posted: Tue Oct 31, 2006 12:07 am
by khurramfraz
We have developed very large php website, using Fusebox framework. we thought during development that all resources all autmatically closed at the end of php page. but it is not in the case of recordset/resultset objects and these objects does not release memory.
Now i want to close all recordsets in entire application , if i do it in every page it takes too much time bec too many pages.
Is there any way that all recordsets are closed when request completed. any simplest way to hadle this problem ?
Thanks in advance
Khurram Fraz
Posted: Tue Oct 31, 2006 1:27 am
by ohlaph
There is really no quick way to close them all...
Posted: Tue Oct 31, 2006 2:38 am
by AKA Panama Jack
Actually they ARE closed on later versions of PHP. If you are using a version of PHP earlier than 4.1 then there is a bug that will cause those resource connections to mysql to stay open. This bug is really noticeable if you are using persistent connections.
But the only good practice is to close all results as soon as you are finished with them just in case there is a problem. Especially if you have a large program because it will free up memory.
BTW, on most servers using persistent connections will not gain you any speed or lower overhead. They did help when servers still well below 1 ghz but there isn't any real benefit to using them anymore. The reason I mentioned this is because versions of PHP before 4.1 had a nasty bug with persistent mysql connections. You couldn't close them and often times they wouldn't be reused and they would stack up until the max connections was hit. Then that happened the mysql server would stop processing requests and a reboot would be needed.
Posted: Tue Oct 31, 2006 4:39 am
by khurramfraz
Dear AKA Panama Jack
Thanks for ur reply.
We are using PHP 5, so should i need to close all recodsets(how ?) or they are automatically closed in PHP version 5 ? wht do u say ?
Thanks
Khurram Fraz
Posted: Tue Oct 31, 2006 11:56 am
by AKA Panama Jack
They should be automatically closed unless another bug has been introduced to the PHP mysql code.
But DO NOT rely on PHP closing result sets for you when the program finishes executing. It is sloppy programming and can come bite you in the butt at a later date. It's best to close all result sets and the database connection before PHP finishes executing the program.
Posted: Tue Oct 31, 2006 12:36 pm
by batfastad
Could you post some code to illustrate the best way to close the connection properly.
Do I need to use both mysql_close and mysql_free_result?
Also, I guess it's ok to close the connection once the query result is obtained in a variable?
... Unless there's multiple queries on a page, in which case I guess just close after the last one to avoid connecting / disconnecting - I guess that would add an overhead.
I just want to make sure I'm doing this correctly!
Thanks
Posted: Wed Nov 01, 2006 11:07 am
by AKA Panama Jack
Use the mysql_free_result to close the result set when you have finished pulling the data created by the query. This will free up the memory taken by the query.
Just before your php program finishes use mysql_close to close the connection between PHP and the mysql server. This will free up memory and a connection slot on the mysql server for another connection.
Posted: Wed Nov 01, 2006 12:49 pm
by batfastad
Ok great
Thanks for the info.