Question about destructor of database and multiple use access

Lodovico Giaretta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 28 07:43:32 PDT 2016


On Thursday, 28 July 2016 at 14:33:26 UTC, Dechcaudron wrote:
> On Thursday, 28 July 2016 at 14:01:45 UTC, Suliman wrote:
>> 2. Should I call destructor and how it's should like?
> You certainly want to close the connection to the db. 
> Basically, the destructor is intended to free resources such as 
> dynamic memory, closing connections... the GC will take care of 
> dynamic memory, but closing the connection to the DB is up to 
> you. So do that in the destructor.

No! Never run important finalization in a class destructor! The 
GC is not obliged to run the destructors, so you may end up with 
your objects destroyed but the connections still open. For this 
kind of important things, you have to do them manually.
This is true of all major programming languages: JDBC (Java DB 
framework, one of the most used in the world) requires calling 
explicit methods to close connections, statements and result 
sets, because the GC cannot be relied upon. The same goes for C# 
sql library.


More information about the Digitalmars-d-learn mailing list