SQL Server`sp_who2`过程通过休眠连接显示长锁

时间:2017-06-24 08:54:00

标签: sql-server azure entity-framework-6 database-connection sp-who2

我正在运行Azure SQL Server,并且正在使用它的WebApi应用程序存在一些问题。在负载较重的某个时刻,应用程序开始创建大量连接,sp_who2显示我有几个长锁。在跟踪锁链后,我可以看到罪魁祸首是sleeping连接,此时不执行任何操作(status ='sleeping'和BlkBy ='。')。

enter image description here

当我进行WebApi进程的内存转储时,我可以看到它们中的大多数都有以下调用堆栈:

[Managed to Native Transition]  
    >   mscorlib.dll!System.Threading.SemaphoreSlim.WaitUntilCountOrTimeout(int millisecondsTimeout, uint startTime, System.Threading.CancellationToken cancellationToken)  Unknown
        mscorlib.dll!System.Threading.SemaphoreSlim.Wait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken) Unknown
        mscorlib.dll!System.Threading.SemaphoreSlim.Wait()  Unknown
        System.Data.dll!System.Data.SqlClient.SqlInternalConnectionTds.SyncAsyncLock.Wait(bool canReleaseFromAnyThread) Unknown
        System.Data.dll!System.Data.SqlClient.SqlInternalConnectionTds.ObtainAdditionalLocksForClose()  Unknown
        System.Data.dll!System.Data.ProviderBase.DbConnectionInternal.CloseConnection(System.Data.Common.DbConnection owningObject, System.Data.ProviderBase.DbConnectionFactory connectionFactory) Unknown
        System.Data.dll!System.Data.SqlClient.SqlConnection.Close() Unknown
        EntityFramework.dll!System.Data.Entity.Infrastructure.Interception.InternalDispatcher<System.Data.Entity.Infrastructure.Interception.IDbConnectionInterceptor>.Dispatch<System.Data.Common.DbConnection, System.Data.Entity.Infrastructure.Interception.DbConnectionInterceptionContext>(System.Data.Common.DbConnection target, System.Action<System.Data.Common.DbConnection, System.Data.Entity.Infrastructure.Interception.DbConnectionInterceptionContext> operation, System.Data.Entity.Infrastructure.Interception.DbConnectionInterceptionContext interceptionContext, System.Action<System.Data.Entity.Infrastructure.Interception.IDbConnectionInterceptor, System.Data.Common.DbConnection, System.Data.Entity.Infrastructure.Interception.DbConnectionInterceptionContext> executing, System.Action<System.Data.Entity.Infrastructure.Interception.IDbConnectionInterceptor, System.Data.Common.DbConnection, System.Data.Entity.Infrastructure.Interception.DbConnectionInterceptionContext> executed) Unknown
        EntityFramework.dll!System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Close(System.Data.Common.DbConnection connection, System.Data.Entity.Infrastructure.Interception.DbInterceptionContext interceptionContext)   Unknown
        EntityFramework.dll!System.Data.Entity.Core.EntityClient.EntityConnection.StoreCloseHelper()    Unknown
        EntityFramework.dll!System.Data.Entity.Core.Objects.ObjectContext.ReleaseConnection()   Unknown
        EntityFramework.dll!System.Data.Entity.Core.Common.Internal.Materialization.Shaper<My.EF6.Entity>.Finally() Unknown
        EntityFramework.dll!System.Data.Entity.Core.Common.Internal.Materialization.Shaper<System.__Canon>.ObjectQueryNestedEnumerator.Dispose()    Unknown
        EntityFramework.dll!System.Data.Entity.Internal.LazyAsyncEnumerator<System.__Canon>.Dispose()   Unknown
        EntityFramework.dll!System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.FirstOrDefaultAsync<My.EF6.Entity>(System.Data.Entity.Infrastructure.IDbAsyncEnumerable<My.EF6.Entity> source, System.Threading.CancellationToken cancellationToken) Unknown
        mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
        mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
        mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()    Unknown
        EntityFramework.dll!System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter<bool>.UnsafeOnCompleted.AnonymousMethod__0() Unknown
        mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask)    Unknown
        mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()  Unknown
        mscorlib.dll!System.Threading.Tasks.Task<bool>.TrySetResult(bool result)    Unknown
        mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<bool>.SetResult(bool result)    Unknown
        EntityFramework.dll!System.Data.Entity.Internal.LazyAsyncEnumerator<My.EF6.Entity>.FirstMoveNextAsync(System.Threading.CancellationToken cancellationToken) Unknown
        mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
        mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
        mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()    Unknown
        EntityFramework.dll!System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter<System.__Canon>.UnsafeOnCompleted.AnonymousMethod__0()   Unknown
        mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask)    Unknown
        mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()  Unknown
        mscorlib.dll!System.Threading.Tasks.Task<System.__Canon>.TrySetResult(System.__Canon result)    Unknown
        mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.Data.Entity.Core.Objects.ObjectResult<My.EF6.Entity>>.SetResult(System.Data.Entity.Core.Objects.ObjectResult<My.EF6.Entity> result)  Unknown
        EntityFramework.dll!System.Data.Entity.Core.Objects.ObjectQuery<My.EF6.Entity>.GetResultsAsync(System.Data.Entity.Core.Objects.MergeOption? forMergeOption, System.Data.Entity.Infrastructure.IDbExecutionStrategy executionStrategy, System.Threading.CancellationToken cancellationToken) Unknown
        mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
        mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
        mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()    Unknown
        EntityFramework.dll!System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter<System.__Canon>.UnsafeOnCompleted.AnonymousMethod__0()   Unknown
        mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask)    Unknown
        mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()  Unknown
        mscorlib.dll!System.Threading.Tasks.Task<System.__Canon>.TrySetResult(System.__Canon result)    Unknown
        mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.Data.Entity.Core.Objects.ObjectResult<My.EF6.Entity>>.SetResult(System.Data.Entity.Core.Objects.ObjectResult<My.EF6.Entity> result)  Unknown
        EntityFramework.SqlServer.dll!System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.ExecuteAsyncImplementation<System.Data.Entity.Core.Objects.ObjectResult<My.EF6.Entity>>(System.Func<System.Threading.Tasks.Task<System.Data.Entity.Core.Objects.ObjectResult<My.EF6.Entity>>> func)  Unknown
        mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
        mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
        mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()    Unknown
        mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask)    Unknown
        mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()  Unknown
        mscorlib.dll!System.Threading.Tasks.Task<System.__Canon>.TrySetResult(System.__Canon result)    Unknown
        mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.Data.Entity.Core.Objects.ObjectResult<My.EF6.Entity>>.SetResult(System.Data.Entity.Core.Objects.ObjectResult<My.EF6.Entity> result)  Unknown
        EntityFramework.dll!System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransactionAsync<System.Data.Entity.Core.Objects.ObjectResult<My.EF6.Entity>>(System.Func<System.Threading.Tasks.Task<System.Data.Entity.Core.Objects.ObjectResult<My.EF6.Entity>>> func, System.Data.Entity.Infrastructure.IDbExecutionStrategy executionStrategy, bool startLocalTransaction, bool releaseConnectionOnSuccess, System.Threading.CancellationToken cancellationToken)   Unknown
        mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
        mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
        mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()    Unknown
        EntityFramework.dll!System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter<System.__Canon>.UnsafeOnCompleted.AnonymousMethod__0()   Unknown
        mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask)    Unknown
        mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()  Unknown
        mscorlib.dll!System.Threading.Tasks.Task<System.__Canon>.TrySetResult(System.__Canon result)    Unknown
        mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.Data.Entity.Core.Objects.ObjectResult<My.EF6.Entity>>.SetResult(System.Data.Entity.Core.Objects.ObjectResult<My.EF6.Entity> result)  Unknown
        EntityFramework.dll!System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.ExecuteAsync<My.EF6.Entity>(System.Data.Entity.Core.Objects.ObjectContext context, System.Data.Entity.Core.Objects.ObjectParameterCollection parameterValues, System.Threading.CancellationToken cancellationToken)   Unknown
        mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
        mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
        mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()    Unknown
        EntityFramework.dll!System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter<System.__Canon>.UnsafeOnCompleted.AnonymousMethod__0()   Unknown
        mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask)    Unknown
        mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()  Unknown
        mscorlib.dll!System.Threading.Tasks.Task<System.__Canon>.TrySetResult(System.__Canon result)    Unknown
        mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.Data.Common.DbDataReader>.SetResult(System.Data.Common.DbDataReader result)  Unknown
        EntityFramework.dll!System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommandsAsync(System.Data.Entity.Core.EntityClient.EntityCommand entityCommand, System.Data.CommandBehavior behavior, System.Threading.CancellationToken cancellationToken)   Unknown
        mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
        mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   Unknown
        mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()    Unknown
        EntityFramework.dll!System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter<System.__Canon>.UnsafeOnCompleted.AnonymousMethod__0()   Unknown
        mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask)    Unknown
        mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()  Unknown
        mscorlib.dll!System.Threading.Tasks.Task.Finish(bool bUserDelegateExecuted) Unknown
        mscorlib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot)    Unknown
        mscorlib.dll!System.Threading.Tasks.Task.ExecuteEntry(bool bPreventDoubleExecution) Unknown
        mscorlib.dll!System.Threading.Tasks.ThreadPoolTaskScheduler.TryExecuteTaskInline(System.Threading.Tasks.Task task, bool taskWasPreviouslyQueued)    Unknown
        mscorlib.dll!System.Threading.Tasks.TaskScheduler.TryRunInline(System.Threading.Tasks.Task task, bool taskWasPreviouslyQueued)  Unknown
        mscorlib.dll!System.Threading.Tasks.TaskContinuation.InlineIfPossibleOrElseQueue(System.Threading.Tasks.Task task, bool needsProtection)    Unknown
        mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()  Unknown
        mscorlib.dll!System.Threading.Tasks.Task<System.__Canon>.TrySetResult(System.__Canon result)    Unknown
        mscorlib.dll!System.Threading.Tasks.UnwrapPromise<System.Data.SqlClient.SqlDataReader>.TrySetFromTask(System.Threading.Tasks.Task task, bool lookForOce)    Unknown
        mscorlib.dll!System.Threading.Tasks.UnwrapPromise<System.__Canon>.ProcessInnerTask(System.Threading.Tasks.Task task)    Unknown
        mscorlib.dll!System.Threading.Tasks.UnwrapPromise<System.__Canon>.Invoke(System.Threading.Tasks.Task completingTask)    Unknown
        mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()  Unknown
        mscorlib.dll!System.Threading.Tasks.Task.Finish(bool bUserDelegateExecuted) Unknown
        mscorlib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot)    Unknown
        mscorlib.dll!System.Threading.Tasks.Task.ExecuteEntry(bool bPreventDoubleExecution) Unknown
        mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch()    Unknown
        [Native to Managed Transition]  

所有阻止/阻止会话都是定期更新,这些更新非常快,并且不会延迟来自客户端的数据库调用。我正在为每个数据库调用使用EF6和using(var db = new DB()) {...}

睡眠连接阻止其他连接可能有什么问题?我该如何调试问题?

0 个答案:

没有答案