Ragnar Hojland Espinosa ragnar@linalco.com wrote:
The thing is.. I'm leaving behind lots open postgres connections which would be nicely closed with a "destructor", and right now I don't know how to automatically close (maybe you cant?) when it should (the sooner the better). Obviously, postgres complains quite loudly when you have too many open, and things suddely get very ugly.
Ah well.. at least I can add the closes by hand :/ but that means leaks and sort of rules out to have decent db connection pools in any way I can think of.. so I must be missing something.
Finalization works as well as it can, but unfortunately that isn't wonderfully well. For handling resources other than memory, e.g. database connections, it is usually better if you can work out a way to do it manually.
In this case, maybe it can be done without needing to put #close's everywhere. Key in on "maintaining a db connection pool". Make an object DBConnectionPool which keeps track of all open connections, and have everyone query this object when they want an open connection to use. Have this object periodically check if it has any idle connections, and close a few if necessary. Similarly, have it open a few whenever that seems appropriate.
I hope this idea works out for your situation. I'm sure this idea is discussed on the WWW -- it's a common problem!
Lex