1. 程式人生 > 其它 >問題"可能由於暫時性失敗引發了異常。如果您在連線到 SQL Azure 資料庫,請考慮使用 SqlAzureExecutionStrategy。"的處理辦法

問題"可能由於暫時性失敗引發了異常。如果您在連線到 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.