Search code examples
c#sql-serversqlconnectionsqlconnection.close

What disadvantages are there for leaving an SQL Connection open?


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


Solution

  • 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.