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

In Java and C #, they have something like System.terminate (). If my program has open database connections, database readers, and database command variables, and I end my program in a catch clause, will the database resources still be used? or will they be released automatically since my entire program has just quit?

As a rule, how should I handle such cases in order to always get free database connections, whether through regular program termination or unexpected program termination? Any good practices?

+6
language-agnostic database database-connection
source share
8 answers

In C #, you usually close connections immediately after using them, for example:

using(SqlConnection connection = ...) { ... do something ... } // connection disposed / closed here 

Typically, you will use a connection pool, and all this returns a connection to the pool. That way, your process will still have active connections to the database server.

If you shut down cleanly, the connection pool will undoubtedly be cleared and the actual database connections will be closed (you can check this by looking at the open connections on the database server).

But there are situations (for example, calling the .FailFast environment) where the application may crash without closing the connection - in this case, they will eventually time out and be closed by the database server.

0
source share

Upon completion of the process, all related resources (including memory, descriptors, connections, ...) will be freed.

Typically in C # you use the Dispose pattern / using statement to manage limited resources.

+6
source share

If you do not close the connection, they will remain open until the timeout hits.

I found this on a hard way in C # several times. Best practices dictate turning off / closing resources that, as you know, you will no longer need. File streams of input-output, connections to DB, etc.

+5
source share

In C #, implicit cleaning is performed by the garbage collector if the finalizer is implemented in an object collected by garbage. Any cleanup of unmanaged resources, such as database connections, can be done in the Dispose method.

See this article for more information:

Implementing Finalize and Dispose to clean unmanaged resources
http://msdn.microsoft.com/en-us/library/b1yfkh5e(VS.71).aspx

+1
source share

When the process ends, all file descriptors that it opened must be freed by the operating system. File descriptors include files and sockets that typically span your database connections.

All that tells you is that when your client completes their connections, they close. He does not tell you what the server is doing. Depending on how it is written, it is possible that the server will continue to open its connections, waiting for messages from a client that never arrives, or even attempts to send data. This may probably end, but it may not be as well planned. (This should be for a decent DBMS, but it may not be so.) Therefore, depending on your RDBMS, you may need several steps to inform the server you are talking about in order to inform it of the release of its resources.

+1
source share

If you work with SQL Server, you can look in sysprocesses or run sp_who2. I tested this on my machine and the connections are closed, i.e.

 Console.Write("Opening connection"); Console.ReadLine(); SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=SeniorMail;Integrated Security=SSPI;"); connection.Open(); SqlCommand command = new SqlCommand("SELECT count(*) from Account", connection); Console.Write("Running sql"); Console.ReadLine(); int? count = command.ExecuteScalar() as int?; Console.Write("Now I'll throw an exception"); Console.ReadLine(); int a = 0, b = 1, c = 0; try { c = b / a; } catch { Environment.Exit(1); } 

I checked sp_who2 on either side of โ€œNow I will throw an exceptionโ€, and I see that the connection disappeared after the application exited.

+1
source share

The general behavior of POSIX is that when the program terminates, all file descriptors, network connections, etc. are closing. Is the other end the correct open question on this question, but if you use any reasonably popular RDBMS, everything will be fine.

0
source share

It should be cleared at the next GC, but to be precise, in C # you can close the connection at the final processing unit of structured exceptions.

 try { // open DB connection } catch (Exception ex) { // deal with exception } finally { // close and dispose connection } 
-one
source share

All Articles