What disadvantages are there for leaving an SQL Connection open?
Asked Answered
P

6

8

This seems to be a simple question, but I wonder the disadvantages of not calling the "close()" function.

Poetry answered 26/1, 2010 at 12:57 Comment(0)
C
7

Apart from exhausting the connection pool (as most answers so far have been), you are in danger of locking data.

If you are reading or writing to a table, some locking semantics will cause certain rows to be locked to other connections. This is especially true if you have any open transaction on the connection.

Reads and writes can then fail and the application will throw exceptions all over the place.

In short, always close the connection.

Copse answered 26/1, 2010 at 13:2 Comment(4)
Even after the sql command using the connection is done with the rows?Hereunto
If you have a transaction open, then yes.Copse
A commit should be enough to avoid that problem, though.Nadeen
You will need to commit all nested transactions.Copse
N
10

Sooner or later, you'll run into the "The Maximum Connection Limit Has Been Reached" error. I'd call that a major disadvantage.

Nadeen answered 26/1, 2010 at 12:58 Comment(0)
C
7

Apart from exhausting the connection pool (as most answers so far have been), you are in danger of locking data.

If you are reading or writing to a table, some locking semantics will cause certain rows to be locked to other connections. This is especially true if you have any open transaction on the connection.

Reads and writes can then fail and the application will throw exceptions all over the place.

In short, always close the connection.

Copse answered 26/1, 2010 at 13:2 Comment(4)
Even after the sql command using the connection is done with the rows?Hereunto
If you have a transaction open, then yes.Copse
A commit should be enough to avoid that problem, though.Nadeen
You will need to commit all nested transactions.Copse
A
1

The connection pool will fill up and any new connections will time out waiting for a new connection from the pool.

Astilbe answered 26/1, 2010 at 12:58 Comment(0)
P
1

Each and every connection to SQL Server requires memory allocation.

So the more connections you have open the more memory that is being used and held, that could potentially be put to better use.

If you want to know just how much memory is used by connections in SQL Server, take a look at the following reference.

Memory Used by SQL Server Objects

Petronius answered 26/1, 2010 at 13:48 Comment(1)
Updated your link to MSDN, but at ~300K per connection, this is probably one of the lesser considerations... it will take thousands, or tens of thousands of connections before this starts making itself felt.Trouble
H
0

a connection to the database server is open. suppose you have 100s of programs hitting the same server...

you should also dispose it

Hereunto answered 26/1, 2010 at 12:59 Comment(0)
O
0

Quick simple answer is that the sooner you close, the sooner the connection can be re-used by the connection pool.

Overbear answered 26/1, 2010 at 12:59 Comment(1)
"using using is good" is a little mantra I say to myself when conencting to sql. SqlConnection implements iDisposable so simply wrap your sql code in a "using" block to ensure your close is called automatically.Overbear

© 2022 - 2024 — McMap. All rights reserved.