title: SQL Azure - The timeout period elapsed during the post-login phase
body: <p>I have azure cloud service - 2 web instances, serving about 500,000 requests per day. Using Azure business database. There is one issue with it - sometimes (avg. once per day, but not in the same time) there is 2-3 minutes long interval while i am unable to query DB, the error message is like this:</p>
  
  <pre><code>System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---&gt; System.Data.SqlClient.SqlException: Connection Timeout Expired.  The timeout period elapsed during the post-login phase.  The connection could have timed out while waiting for server to complete the login process and respond; Or it could have timed out while attempting to create multiple active connections.  The duration spent while attempting to connect to this server was - [Pre-Login] initialization=1; handshake=12; [Login] initialization=0; authentication=0; [Post-Login] complete=14004;  ---&gt; System.ComponentModel.Win32Exception: The wait operation timed out
     --- End of inner exception stack trace ---
     at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection)
     at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection)
     at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection)
     at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
     at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
     at System.Data.SqlClient.SqlConnection.Open()
     at System.Data.Entity.Infrastructure.DbExecutionStrategy.&lt;&gt;c__DisplayClass1.&lt;Execute&gt;b__0()
     at System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute[TResult](Func`1 operation)
     at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
     --- End of inner exception stack trace ---
     at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
     at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection()
     at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
     at System.Data.Entity.Core.Objects.ObjectQuery`1.&lt;&gt;c__DisplayClassb.&lt;GetResults&gt;b__9()
     at System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute[TResult](Func`1 operation)
     at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
     at System.Data.Entity.Core.Objects.ObjectQuery`1.&lt;System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator&gt;b__0()
     at System.Lazy`1.CreateValue()
     at System.Lazy`1.LazyInitValue()
     at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
     at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
     at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
     at MyProject.MyRepository.GetItems(Boolean includePremium, Int32 a, Int32 b, Int32 c)
     at MyProject.Controllers.MyController.GetItems(String Id, String device)
  </code></pre>
  
  <p>Numbers in the message - [Pre-Login] initialization=1; handshake=12; [Login] initialization=0; authentication=0; [Post-Login] complete=14004 - are always the same, not some random wait times or something... </p>
  
  <p>Of course I tried to google this error, but all I could find was related to wrong connection management with plain ADO.NET etc. I am using Entity Framework 6, with code-first model, linq to entities queries, queries are not heavy (few ms, always under 1s). </p>
  
  <p>There is this question on SO : <a href="https://stackoverflow.com/questions/17215377/connection-timeout-in-sql-azure">Connection timeout in SQL Azure</a> with answer like "this is transient error, just try again" - but this wont solve it for me, I got hundreds of these errors within this interval, I am pretty sure every request fail, so "wait 5 sec and try again" policy wont help here.</p>
  
  <p>Is there something special about Azure DB which I must do about connection lifetime management in EF? Or you think this can be error message for "throttling" DB?</p>