"可能由於暫時性失敗引發了異常。如果您在連接到 SQL Azure 數據庫,請考慮使用 SqlAzureExecutionStrategy。"問題的處理辦法


Web API接口訪問量突然劇增,Entity Framework框架提示錯誤“可能由於暫時性失敗引發了異常。如果您在連接到 SQL Azure 數據庫,請考慮使用 SqlAzureExecutionStrategy。”,錯誤堆棧如下:

--- 引發異常的上一位置中堆棧跟蹤的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   在 System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesInternalAsync>d__31.MoveNext()
--- 引發異常的上一位置中堆棧跟蹤的末尾 ---
   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

數據無法保存,可是重試幾次保存數據又是成功的。記錄一下排查問題的經過:

1、Entity Framework代碼的問題

可是是並發鎖引起的問題,保存數據增加線程鎖。傍晚訪問人數比較少,基本上沒有提示錯誤,等到晚一點又提示錯誤。這個調整失敗。

查詢接口與提交接口在相同控制器,遷移查詢API到另外控制器,一夜無事。可是第二天,訪問量上來之后,有提示錯誤。這個調整失敗。

2、數據庫密碼的問題

在csdn找到一篇文字,修改數據庫用戶名,文章鏈接。對於該錯誤,此方案完全無用。

3、dba查看數據庫

數據庫日志、操作系統日志正常,SQL Server數據庫運行正常。dba干掉了訂閱業務(數據同步業務),做了定時備份。對於該錯誤,dba也幫不上忙。不懂為什么dba沒有想到查看數據庫鎖表問題。

4、數據庫鎖表

這是一個很大的突破,開始接近問題真相了。使用SQL Server Profiler工具,查看鎖表情況,入下圖所示:

觀察到錯誤及其類似語句:

Parallel query worker thread was involved in a deadlock

鎖表語句:

(@0 int,@1 nvarchar(32),@2 datetime2(7),@3 nvarchar(64),@4 int,@5 int,@6 bit)
insert [dbo].[CommandRequests](
   [CommandId]
 , [DeviceId]
 , [StartDateTime]
 , [EndDateTime]
 , [Parameters]
 , [Caller]
 , [Result]
 , [Priority]
 , [Timeout]
 , [ParentRequestId]
 , [IsSuccessful]
 , [Host])
  values (@0, @1, @2, null, null, @3, null, @4, @5, null, @6, null)

  select [CommandRequestId]
  from [dbo].[CommandRequests]
  where @@ROWCOUNT &gt; 0 and [CommandRequestId] = scope_identity()   

Stack Overflow找到一個同樣的問題,有個回復真是救人於水火之中呀!摘錄如下:

“I think the reason is CommandRequestId is not the primary key. If you set it as primary key you will not get dead lock. I had the same issue and when I set the Identity column as primary key it worked fine.”

最后看到就是ID主鍵的問題,id是自增的,但是沒有設置主鍵。在遷移服務到雲上忘記設置了,忘記設置了.......

排查超過36個小時的問題,終於搞定了!感謝那些亂分享的同行,也感謝Stack Overflow.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM