2016-07-13 26 views
0

我不能讓SqlConnection.RetrieveStatistics工作,它總是返回18個元素是全零的哈希表。如何使SqlConnection.RetrieveStatistics正常工作?

我想做什麼:在我的測試environnent,我想添加一個標題爲每個HttpResponse對象與請求處理過程中執行的SQL命令的數量。

只有我的應用程序(REST)Web服務,我有一個SQL連接不相上下請求(每個請求一個NHibernate的會議)。

的請求被處理後,我做的:

var connection = statelessSession.NHSession.Connection as ReliableSqlDbConnection; 
if (connection != null) 
    queries += Convert.ToInt32(connection.ReliableConnection.Current.RetrieveStatistics()["Prepares"]); 

數總是0,而當我在這條線斷,我看到所有的數字都是零。

會話打開後,我做的:

var ret = module.OpenSession(); 
var connection = (ret.Connection as ReliableSqlDbConnection); 
if(connection != null) 
{ 
    connection.ReliableConnection.Current.StatisticsEnabled =true; 
    ret.CreateSQLQuery("SET STATISTICS IO ON; SET STATISTICS TIME ON;").ExecuteUpdate(); 
    connection.ReliableConnection.Current.ResetStatistics(); 
} 
return ret; 

我使用https://github.com/MRCollective/NHibernate.SqlAzure因爲在生產中我使用SQL Azure的,但在本地I具有SQL Server 2014開發版的一個實例。

對於我的數據庫,我有: 自動創建統計真實 自動更新的統計數據真實

我錯過了什麼?

回答

0

這樣做的原因是,NHibernate的打開爲每個請求一個新的連接,這就是爲什麼我失去每一次的統計數據。