Do not depend on the garbage collector
aviks opened this issue · 2 comments
I think it is a bad idea to depend on the garbage collector to close connections. Even though you say "it is best practice to close connections early", users will still expect that it would be correct to not close connections. However, the GC does not guarantee when finalisers will run, or if it will at all.
So while finalisers are useful in some circumstances, external connections should always be closed explicitly.
Thanks for the input, @aviks!
The way I implemented this, the user can always close things explicitly. But if the user forgets to do so, the GC will close things automatically. Also, the use of do-syntax in some methods is to close statements explicitly behind the scenes. So I guess there is no harm with the current setup. Perhaps I can just remove this note from the docs.
Yes, it is fine to have that as backup. My main advice, I suppose, is to invert the way this is documented. Instead of saying "Connections are automatically closed, but you should close them yourself", as it is now, say something on the lines of "Connections should be closed explicitly. The finaliser will close any open connections it finds, but there is not guarantee of when that is run." (just summarising)