In Java and C#, they both have something like System.terminate(). If my program has open database connections, database readers, and database command variables, and I terminate my program in a catch clause, will database resources still remain in use? or will they be freed automatically since my entire program has just exited?
Normally, how should I handle such cases to make sure I always free database connections, whether through normal program termination or unexpected program termination? Any good practices?
In C#, you will normally close connections immediately after using them, e.g.:
However typically you will be using a connection pool, and all this does is returns the connection to the pool. So your process will still have active connections to the database server.
If you shut down cleanly, the connection pool will no doubt be cleaned up, and the actual connections to the database will be closed (you can verify this by looking at the open connections on the database server).
But there are situations (e.g. calling Environment.FailFast) where the application may crash without closing down the connections – in this case they’ll eventually time out and be closed by the database server.