我有與連接字符串Asynchronous Processing=true;
和MultipleActiveResultSets=True;
爲什麼sqlCommand.EndExecuteNonQuery()被阻止?
的應用程序有多個線程,每個線程打開自己的連接來訪問數據庫連接到SQL Server 2008 R2的C#應用程序。
大多數的應用程序的工作時間,但偶爾有些線程被封鎖在下面的代碼:
IAsyncResult asyncResult = sqlCommand.BeginExecuteNonQuery();
int rowsAffected = sqlCommand.EndExecuteNonQuery(asyncResult);
sqlCommand.EndExecuteNonQuery()
永遠不會返回,或退出時間。
我檢查堆棧跟蹤:
Child SP IP Call Site
000000000242d608 00000000778618ca [HelperMethodFrame_1OBJ: 000000000242d608] System.Threading.WaitHandle.WaitOneNative(System.Runtime.InteropServices.SafeHandle, UInt32, Boolean, Boolean)
000000000242d740 000007fef795bf64 System.Threading.WaitHandle.InternalWaitOne(System.Runtime.InteropServices.SafeHandle, Int64, Boolean, Boolean)
000000000242d780 000007fee438bed6 System.Data.SqlClient.SqlCommand.WaitForAsyncResults(System.IAsyncResult)
000000000242d7c0 000007fee438baf1 System.Data.SqlClient.SqlCommand.EndExecuteNonQuery(System.IAsyncResult)
我已經設置了sqlCommand.CommandTimeout
到30秒,但我不明白,爲什麼這個問題偶爾會發生。
我檢查了SQL Server是否阻塞了會話,但沒有。是否有可能是因爲同一個應用程序擁有多個連接,並且它們彼此阻塞?
你明白,通過調用BeginXyz,然後*立即*調用EndXyz,你基本上做同樣的Xyz調用? –
謝謝你指出,這是正確的。我將不得不看看代碼的歷史,看看爲什麼這樣。否則,我會將其更改爲sqlCommand.ExecuteNonQuery() – dsum