Search code examples
windowsserviceado.netsqlconnection

Are there drawbacks to leaving a SqlConnection open long-term?


I am creating a MS Windows service, which listens for TCP connections. When connected, it gets data from a SQL db and returns it via the TCP socket. What are the drawbacks, if any, of opening a SqlConnection to the SQL Server at service start time, and just re-using that, until it might fail, as opposed to opening a new connection each time a "request" is made? I expect a small number of instantiations of the service functionality - less than 10 a day, but it could be more than that.


Solution

  • Database connections are considered an "expensive" resource, and as such should be opened only when needed, and closed immediately thereafter. As a result, opening a connection early and persisting it would go against that philosophy. Additionally, doing so prevents your underlying framework from making best use of whatever variety of connection pooling it may implement. It just isn't a very scalable practice.