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
Forgot to close Recordsets at each page, Wht to do now
Moderator: General Moderators
-
khurramfraz
- Forum Newbie
- Posts: 2
- Joined: Mon Oct 30, 2006 11:51 pm
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
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.
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.
-
khurramfraz
- Forum Newbie
- Posts: 2
- Joined: Mon Oct 30, 2006 11:51 pm
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
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.
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.
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
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
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
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.
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.