title: Multiple Simultaneous SQL Connection Timeouts In Multithreaded Windows Service
body: <p>I have a multithreaded Windows Service I've developed with VS 2010 (.NET 4.0) which can have anywhere from a few to a few dozen threads, each retrieving data from a slow server over the Internet and then using a local database to record this data (so the process is Internet-bound, not LAN or CPU bound).</p>
  <p>With some regularity, I am getting a flood/flurry/burst of the following error from several threads simultaneously:</p>
  <blockquote>
  <p>System.Data.SqlClient.SqlException (0x80131904): Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.</p>
  </blockquote>
  <p>The call stack for this error is typically:</p>
  <blockquote>
  <p>at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)</p>
  <p>at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)</p>
  <p>at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)</p>
  <p>at System.Data.SqlClient.SqlConnection.Open()</p>
  </blockquote>
  <p>I'm not specifying a Connection Timeout in the connection string, and there are other applications and processes working in this database.  Has anyone come across this kind of behavior and if so what was done to prevent it?</p>
  <p>The most commonly-called method in my data access layer looks like this, and all my other DAL methods follow the same approach:</p>
  <pre><code>using (SqlConnection con = new SqlConnection(GetConnectionString()))
  using (SqlCommand cmd = new SqlCommand(&quot;AddGdsMonitorLogEntry&quot;, con))
  {
      cmd.CommandType = CommandType.StoredProcedure;
  
      /* setting cmd.Parameters [snipped] */
  
      // We have been getting some timeouts writing to the log; wait a little longer than the default.
      cmd.CommandTimeout *= 4;
  
      con.Open();
  
      cmd.ExecuteNonQuery();
  }
  </code></pre>
  <p>Thanks very much!</p>
  <p><strong>EDIT</strong></p>
  <p>Given comments about this occurring in mirrored environments, I should indeed mention that the database in question is mirrored.  It's marked in SSMS as &quot;Principal, Synchronized&quot;, in &quot;High safety without automatic failover (synchronous)&quot; mode.</p>
  <p><strong>EDIT 5/26/11</strong></p>
  <p>I am seeing nothing in the SQL Server logs to indicate any problems.  (I don't have access to the Windows Event Viewer on that server, but I've asked for someone to look for me.)</p>