2016-04-14 28 views
0

我有一個非常繁忙的應用程序,它通過大約10-20個連接(通過sp_who2)哼哼,但偶爾連接會激增(300-500),並且Azure Sql數據庫將開始終止他們。這會殺死應用程序。它很少發生,但現在更多的是我們添加了第三個應用程序。檢測Azure Sql數據庫上的泄漏連接

目前有4種不同的應用程序打到數據庫 - 一個管理站點和三個不同的應用程序實例。

因此,如果沒有Sql Server Profiler,如何確定泄漏來自哪裏?另外,應用程序主要使用實體框架6,但一些更復雜/使用頻繁的數據訪問方法使用一些自定義存儲過程和ado.net代碼。這裏有一個例子,如果任何人都可以判斷這是一個潛在的問題:

public Content FindContent(int id) 
{ 
    Content content = null; 
    using (SqlCommand command = CreateProcedure("dbo.FindContent")) 
    { 
     AddParam(command, "Id", SqlDbType.Int, id); 

     var results = ExecuteReader<Content, Content, ContentFile>(command, x => BindContent(x), x => BindContent(x), x => BindContentFile(x)); 
     if (results.Result1.Count > 0) 
     { 
      content = results.Result1[0]; 
      content.AttachRelatedItems(results.Result2); 
      content.Files = results.Result3; 
     } 
    } 
    return content; 
} 

BindContent和BindContentFile與結果執行的功能。他們只是循環閱讀並建立收藏。

這需要一個通用的ExecuteReader方法,它處理從操作3分的結果集:

private ThreeResults<T1, T2, T3> ExecuteReader<T1, T2, T3>(SqlCommand command, Func<SqlDataReader, T1> rowConverter1, Func<SqlDataReader, T2> rowConverter2, Func<SqlDataReader, T3> rowConverter3) 
{ 
    ThreeResults<T1, T2, T3> resultsets = new ThreeResults<T1, T2, T3>(); 
    using (SqlConnection connection = new SqlConnection(this.ConnectionString)) 
    { 
     command.Connection = connection; 
     connection.Open(); 
     using (SqlDataReader reader = command.ExecuteReader()) 
     { 
      List<T1> ret1 = new List<T1>(); 
      List<T2> ret2 = new List<T2>(); 
      List<T3> ret3 = new List<T3>(); 
      while (reader.Read()) 
      { 
       ret1.Add(rowConverter1(reader)); 
      } 

      reader.NextResult(); 
      while (reader.Read()) 
      { 
       ret2.Add(rowConverter2(reader)); 
      } 

      reader.NextResult(); 
      while (reader.Read()) 
      { 
       ret3.Add(rowConverter3(reader)); 
      } 

      resultsets.Result1 = ret1; 
      resultsets.Result2 = ret2; 
      resultsets.Result3 = ret3; 

      reader.Close(); 
     } 
    } 
    return resultsets; 
} 

回答