title: Dapper async/await connection timeout
body: <p>We're running into an issue were we execute a large number of queries at once (large being 100-200) using Dapper async and end up getting timeout exceptions. I think what is happening is that Dapper is doing <code>await ... [connection].OpenAsync(...)</code> (<a href="https://github.com/StackExchange/Dapper/blob/master/Dapper/SqlMapper.Async.cs#L388" rel="nofollow noreferrer">seen here</a>) which is allowing other asynchronous code to also open connections, then by the time the code gets back to the original opened connection to start he query the sql timeout has already elapsed. My question there a way or practice to prevent this. One solution we've considered is just increasing the sql timeout. Another has been to reduce the number of object's we're querying from the DB at once. However, it just feels like we're not doing it right and wanted to reach out and see if there was a better way. Thanks in advance and sorry if this is a duplicate, I couldn't find any other describing a solution to this problem.</p>
  
  <p>Here is the code executing the query:</p>
  
  <pre><code>public async Task&lt;bool&gt; ThingExistsAsync(int? somethingId, int somethingTypeId)
  {
      using (var conn = connectionFactory.GetEddsPerformanceConnection())
      {
          return await conn.QueryFirstOrDefaultAsync&lt;int&gt;(Resources.Event_ReadCountBySomethingTypeAndId,
                  new { somethingId, somethingTypeId }) &gt; 0;
      }
  }
  </code></pre>
  
  <p>Calling code:</p>
  
  <pre><code>Task.WhenAll(listOfThings.Select(t =&gt; ThingExistsAsync(t.Id, t.Type)));
  </code></pre>
  
  <p>Here is the exception:</p>
  
  <pre><code>System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. ---&gt; System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out
     at System.Data.SqlClient.SqlCommand.&lt;&gt;c.&lt;ExecuteDbDataReaderAsync&gt;b__174_0(Task`1 result)
     at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
     at System.Threading.Tasks.Task.Execute()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Dapper.SqlMapper.&lt;QueryRowAsync&gt;d__24`1.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at Company.Product.Data.Repositories.ThingRepository.&lt;ThingExistsAsync&gt;d__17.MoveNext()
  ...
  ClientConnectionId:059604ea-1123-4d8d-ba00-071ed3a0b962
  Error Number:-2,State:0,Class:11
  </code></pre>