Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to an unclosed database connection?

Earlier today I've found a bug in one of our projects - there is a conenction with database that is never closed, i mean the Close() method is never called. However, when I close the application, the connection is closed (checked in sql management studio many times). Why?

like image 819
agnieszka Avatar asked Jun 19 '09 14:06

agnieszka


People also ask

What happens if you don't close a database connection?

If you don't close it, it leaks, and ties up server resources. @EJP The connection itself might be thread-safe (required by JDBC), but the applications use of the connection is probably not threadsafe. Think of things like different transaction isolation, boundaries (commit/rollback/autocommit) etc.

What will happen if we try to open a connection to a database that is already open?

Also if you try to open an opened connection it will throw error so you have to check for that better is either write it in using block or close the connection yourself.

Do I need to close DB connection?

If you do not close your database connections, many problems can occur like web pages hanging, slow page loads, and more. Think of it as going through a door to your house. Maybe the door will shut by itself, but maybe it won't. If it doesn't shut, who knows what will happen.

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.


2 Answers

The connection will close when the application exits. Read up on SqlConnection's Finalize. From MSDN's documentation for Object.Finalize:

"During shutdown of an application domain, Finalize is automatically called on objects that are not exempt from finalization, even those that are still accessible."

like image 79
RichardOD Avatar answered Oct 08 '22 18:10

RichardOD


Another thing to keep in mind here is that in .Net, you can wrap your connections in a using block, and that will close and dispose your connections for you. So the lack of an explicit Close() isn't a bad thing if you've got your using blocks there...

// this using block will auto close & dispose your connection...
using (var conn = new SqlConnection(...))
{
    conn.Open();
    // database code here with no explicit close

}

that is the functional equivalent of a try/finally block with a conn.close in the finally. Many devs overlook the using blocks - make sure you're not doing the same in this case.

If you do rewrite your code to close your connections - it's good practice to use Using blocks around all of your database objects (connection, command, reader) to make sure that they are closing and disposing when they fall out of scope of the using block. I'd definitely suggest writing those into your code instead of just conn.Close() where needed.

like image 32
Scott Ivey Avatar answered Oct 08 '22 17:10

Scott Ivey