簡體   English   中英

在.net中人為地創建連接超時錯誤

[英]Artificially create a connection timeout error in .net

我正在使用asp.net MVC框架,並使用EntityFramework連接到數據庫。 有時在連接數據庫時出現超時錯誤。 這些錯誤非常罕見。

System.Data.Entity.Infrastructure.DbUpdateException:
 An error occurred while updating the entries. See the inner exception for details. ---> System.Data.UpdateException: An error occurred while updating the entries. 
See the inner exception for details. ---> System.Data.SqlClient.SqlException:
Timeout expired. The timeout period elapsed prior to 
completion of the operation or the server is not responding.

var myList = new List<MyConnection>();
using (var db = new MyDb())
            {
                var activeConns = db.Connections
                                    .Where(c => c.Key == tKey)
                                    .Where(c => c.IsActive)
                                    .ToList();
                if (activeConns.Count > 0)
                {
                    //Some custom logic
         myList.add(conn);
                }

            return myList;
        }

我沒有設置任何連接超時期限。 使用實體框架時是否有最佳實踐?

我如何人為地產生這種缺陷,以便可以測試我們的軟件?

我們在代碼庫中處理此類問題的方式是使用模擬對象 -允許我們將真實的實現(例如數據庫)換成僅用於測試的偽實現。

從客戶端的角度來看,偽造者的行為相同,但是我們對測試錯誤等行為的行為擁有更多的控制權(例如,對第5個請求拋出超時錯誤)。

模擬實體框架上下文是另一個SO問題,它對如何進行設置(特別是針對EntityFramework)進行了更為詳細的介紹。

您可能需要使用反射來引發SQL超時異常。 查看本文以獲取幫助: http : //blog.developers.ba/post/2009/03/08/SqlException-class-doesnt-have-public-constructor.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM