This topic contains 3 replies, has 3 voices, and was last updated by  Garry Christensen 9 years, 2 months ago.

Explicit db.close()

  • I am troubleshooting some apparently random database connection issues and I noticed that I was not explicitly calling the db.close() to close the database connection. The connection object is local in scope so it ceases to exist at the end of the function.

    Is the close() called implicitly when the connection object is released? If not, what are the implications if this is not done?


    Hi Gary!

    I think the garbage collector will eventually close the connection. Calling db.close immediately will mean that the connection is closed faster in a more deterministic manner so I would recommend it.

    A related question:

    I haven’t performed any benchmarks, but I was wondering whether it makes sense from a performance standpoint to establish the database connection(s) outside of the main() function, then verify that it’s still active in the main() function vs. opening and closing the connection in main() and thus each time the translator is called. Is it safe to assume that any variables/functions defined outside of main() are persistent until the channel is restarted?

    Jeff Drumm ◊ VP and COO ◊ HICG, LLC. ◊

    Thanks Eliot. I think good coding dictates that an explicit Close() should be used rather than relying on garbage collection. I’ve changed it over to close properly now.

    Jeff, I used that exact approach during over the last 24 hours (without any problem). Yes, the connection object has global scope when defined outside Main(). I don’t have any benchmarking but logically it should be more efficient, particularly in heavy use. I too would be interested in hearing if anyone has any further information.

You must be logged in to reply to this topic.