Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If a console program terminates, will the database connections used in the program still remain open?

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?

like image 377
Saobi Avatar asked Jun 06 '09 21:06

Saobi


People also ask

What happens if database connection is not closed?

If we don't close the connection, it will lead to connection memory leakage. Until application server/web server is shut down, connection will remain active, even if the user logs out.

What happens when SQL connection is closed?

If connection pooling is off, the transaction is rolled back after SqlConnection. Close is called. Transactions started through System. Transactions are controlled through the System.

Why database connections must be closed?

For the purpose of safe coding, you should always close database connections explicitly to make sure that the code was able to close itself gracefully and to prevent any other objects from reusing the same connection after you are done with it.

Why is it good practice to disconnect your database connection when you're finished with it?

It's a good idea to close the connection when you're done using it to ensure that your changes actually go to the store. A power loss after a write and before a flush will lose data.


1 Answers

Upon termination of a process, all associated resources (incl. memory, handles, connections, ...) will be freed up.

Normally, in C#, you'll use the Dispose pattern/using statement to control scarce resources.

like image 88
mmx Avatar answered Sep 22 '22 01:09

mmx