Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if I leave a database connection open in an ASP.NET web page

Suppose that I have an ASP.NET page. In the page load event handler, I open a database connection and do some processing. But after the processing is done, I don't close the connection explicitly by calling the CLOSE method of the connection object.

Now when the page processing at the server side is finished, the GC will dispose all the variables in my page, and also, the connection object too. But when it is disposed, does the connection that was opened previously is automatically closed? I mean, when GC disposes the connection object, does it automatically close the connection that was established with the database server; or it simply dispose the connection object, and the connection at the database is remained open, until the connection timeout occurs at the database and then the database server closes the connection by itself?

like image 501
MD Sayem Ahmed Avatar asked Feb 09 '10 05:02

MD Sayem Ahmed


People also ask

What happens if database connection is not closed?

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 happen if we keep connection open?

While the specific details matter in general there is nothing wrong with keeping connections open for long durations. If your application is connection pooling - the connection slots in the pool typically remain connected until they are needed.

Should I close database 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.

Should you keep SQL connection open?

From MSDN, "Note If the SqlConnection goes out of scope, it is not closed. Therefore, you must explicitly close the connection by calling Close or Dispose." Cheers!


2 Answers

The MSDN documentation is pretty clear about this:

If the SqlConnection goes out of scope, it won't be closed. Therefore, you must explicitly close the connection by calling Close or Dispose. Close and Dispose are functionally equivalent.

Either use the using blocks to have it disposed automatically, or explicitly .Close() it. The using blocks are preferred.

By leaving connections open your application may eventually run out of connections when new requests are attempted, resulting in errors. I've faced such a problem in an application I was debugging. The original developers failed to close the connections explicitly on a few pages and traffic was high enough that users started getting errors. I wrapped the offending connections in a using block and the problem went away.

like image 72
Ahmad Mageed Avatar answered Sep 29 '22 17:09

Ahmad Mageed


Connection is remained open. If you have lots of pageviews, and lots open connections, you can get 500 error.

like image 40
x2. Avatar answered Sep 29 '22 18:09

x2.