title: Dapper.NET Connection/Query Best Practice
body: <p>So i've read a bunch of links/SO questions, but i still can't get a clear answer on this.</p>
  
  <p>When performing SQL queries with Dapper in an ASP.NET application, what is the best practice for opening/closing connections?</p>
  
  <p>Here's the pattern i'm currently following:</p>
  
  <pre><code>using (var db = new SqlConnection(_connectionString))
  {
      return db.Query("dbo.SomeQuery");
  }
  </code></pre>
  
  <p>Essentially, open/close a SQL connection each time as needed.</p>
  
  <p>From my understanding, the above code should automatically open/close the SQL connection (e.g i don't need to explicity do <code>db.Open</code> or <code>db.Close</code>).</p>
  
  <p>The problem i'm seeing is that after a while, i'm getting a bunch of these errors:</p>
  
  <blockquote>
    <p>InvalidOperationExceptionTimeout expired. The timeout period elapsed
    prior to obtaining a connection from the pool. This may have occurred
    because all pooled connections were in use and max pool size was
    reached.</p>
  </blockquote>
  
  <p>I've had a SQL profiler trace during this time period, and i'm not seeing any long queries that would block anything, so it appears as though my ASP.NET web application is running out of connections (as opposed to taking too long to execute a query).</p>
  
  <p>Can someone tell me what i'm doing wrong?</p>
  
  <p>A side note: my application is running as an Azure Web App, so unfortunately i can't actually see how many connections are being opened by the web app. :(</p>