2017-07-04 62 views
0

上午在一個項目上工作,這是基於Web的應用程序許多開發人員都使用連接對象,但他們沒有關閉它。asp.net c#如何知道有多少個連接對象當前打開,但沒有被網頁關閉

,所以我可以我得到網頁上的細節,有多少連接對象正在使用,但沒有關閉

正在使用一些代碼,是在這裏提到

protected void btnCheckConnection_Click(object sender, EventArgs e) 
     { 
      GetIntegratedSecurityConnectionString(); 
      SetUpPerformanceCounters();   
      CreateConnections(); 
     } 

private static string GetIntegratedSecurityConnectionString() 
     { 
      string @SqlConn = "Server=190.196.19.12;UID=test1;PWD=test1;Database=test"; 
      return @SqlConn; 
     } 



private void CreateConnections() 
    { 
     // List the Performance counters. 
     WritePerformanceCounters(); 

     // Create 4 connections and display counter information. 
     SqlConnection connection1 = new SqlConnection(
       GetIntegratedSecurityConnectionString()); 
     connection1.Open(); 
     Console.WriteLine("Opened the 1st Connection:"); 
     WritePerformanceCounters(); 

     SqlConnection connection2 = new SqlConnection(
       GetSqlConnectionStringDifferent()); 
     connection2.Open(); 
     Response.Write("Opened the 2nd Connection:"); 
     WritePerformanceCounters(); 

     SqlConnection connection3 = new SqlConnection(
       GetSqlConnectionString()); 
     connection3.Open(); 
     Console.WriteLine("Opened the 3rd Connection:"); 
     WritePerformanceCounters(); 

     SqlConnection connection4 = new SqlConnection(
       GetSqlConnectionString()); 
     connection4.Open(); 
     Console.WriteLine("Opened the 4th Connection:"); 
     WritePerformanceCounters(); 

     connection1.Close(); 
     Console.WriteLine("Closed the 1st Connection:"); 
     WritePerformanceCounters(); 

     connection2.Close(); 
     Console.WriteLine("Closed the 2nd Connection:"); 
     WritePerformanceCounters(); 

     connection3.Close(); 
     Console.WriteLine("Closed the 3rd Connection:"); 
     WritePerformanceCounters(); 

     connection4.Close(); 
     Console.WriteLine("Closed the 4th Connection:"); 
     WritePerformanceCounters(); 
    } 


private enum ADO_Net_Performance_Counters 
     { 
      NumberOfActiveConnectionPools, 
      NumberOfReclaimedConnections, 
      HardConnectsPerSecond, 
      HardDisconnectsPerSecond, 
      NumberOfActiveConnectionPoolGroups, 
      NumberOfInactiveConnectionPoolGroups, 
      NumberOfInactiveConnectionPools, 
      NumberOfNonPooledConnections, 
      NumberOfPooledConnections, 
      NumberOfStasisConnections, 
      NumberOfFreeConnections 
      // The following performance counters are more expensive to track. 
      // Enable ConnectionPoolPerformanceCounterDetail in your config file. 
      //  SoftConnectsPerSecond 
      //  SoftDisconnectsPerSecond 
      //  NumberOfActiveConnections 

     } 



    private void SetUpPerformanceCounters() 
     { 
      connection.Close(); 
      this.PerfCounters = new PerformanceCounter[11]; 
      string instanceName = GetInstanceName(); 
      testInstanceName = instanceName; 
      Type apc = typeof(ADO_Net_Performance_Counters); 
      int i = 0; 
      foreach (string s in Enum.GetNames(apc)) 
      { 
       this.PerfCounters[i] = new PerformanceCounter(); 
       this.PerfCounters[i].CategoryName = ".NET Data Provider for SqlServer"; 
       this.PerfCounters[i].CounterName = s; 
       this.PerfCounters[i].InstanceName = instanceName; 
       i++; 
      } 
     } 
     [DllImport("kernel32.dll", SetLastError = true)] 
     static extern int GetCurrentProcessId(); 


private string GetInstanceName() 
     { 
      //This works for Winforms apps. 
      /* 
      string instanceName = 
       System.Reflection.Assembly.GetEntryAssembly().GetName().Name; 
       */ 
      //This works for Web apps. 

      //string instanceName = System.Environment.UserDomainName; //System.Reflection.Assembly.GetEntryAssembly().GetName().Name; 
      string instanceName = System.Reflection.Assembly.GetCallingAssembly().GetName().Name; 

      // Must replace special characters like (,), #, /, \\ 
      string instanceName2 = 
       AppDomain.CurrentDomain.FriendlyName.ToString().Replace('(', '[') 
       .Replace(')', ']').Replace('#', '_').Replace('/', '_').Replace('\\', '_'); 

      // For ASP.NET applications your instanceName will be your CurrentDomain's 
      // FriendlyName. Replace the line above that sets the instanceName with this: 
      // instanceName = AppDomain.CurrentDomain.FriendlyName.ToString().Replace('(','[') 
      // .Replace(')',']').Replace('#','_').Replace('/','_').Replace('\\','_'); 

      string pid = GetCurrentProcessId().ToString(); 
      instanceName = instanceName + "[" + pid + "]"; 
      Console.WriteLine("Instance Name: {0}", instanceName); 
      Console.WriteLine("---------------------------"); 
      return instanceName; 
     } 

    private void WritePerformanceCounters() 
     { 
      foreach (PerformanceCounter p in this.PerfCounters) 
      {  
       string strName = p.CounterName + " = " + p.NextValue();  
      }  
     } 

如果有趣p.NextValue( )然後面臨的錯誤附加信息:實例'Dummy_Project [9544]'在指定的類別中不存在。

+0

只要數據已被讀取/處理並且保持打開狀態,連接應該關閉。 SqlConnection也實現了iDisposable,所以你應該總是使用using-statement來進行適當的內存清理。 – Esko

回答

1

在Web應用程序中,請務必儘快關閉連接。告訴你的團隊中的所有其他開發人員也這樣做。

您需要將您的調用包裝在使用語句中(或者如果您希望處理異常,請嘗試... catch),並且它會自動關閉並處理它。 這不會真正關閉連接。它會將它返回到連接池,並且下一次嘗試打開新連接(在很短的時間內)時,實際上會從池中獲得連接。這將顯着提高性能並允許您的應用程序更好地擴展。

+0

絕對你是對的目前所有的開發者都遵循相同的,但我們可以爲以前的代碼做 – ashish

+0

回去並修復它。否則,你打算如何關閉所有這些連接?如果有例外呢?在交通高峯時段,您的申請會跪下。 –

+0

開發人員已關閉連接,但某些連接未關閉,因此您可以提供一些想法,瞭解我們如何識別連接未關閉的位置。 – ashish

相關問題