1

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

在此处输入图像描述

当我对 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()) {...}每个 db 调用。

阻塞其他人的睡眠连接可能有什么问题?如何调试问题?

4

0 回答 0