2011-04-12 40 views
1

所以...我有一些靜態類與一些方法,它返回一些數據到我的數據表。Busy DataReader打破我的網站

問題是多訪問到這個方法。何時不同的客戶端會話在同一時間調用此方法 DataReaded失敗,錯誤我需要關閉當前命令(DataReader),然後再啓動新的。我甚至試圖用但這沒有任何意義。

private RepGroupsDataSourcelocker = Object() 
    public RepGroupsDataSource() : array[(string * int)] 
     lock(RepGroupsDataSourcelocker) 
      def res = linq<#from pr in _Rep_Permission 
        from rg in _Rep_Group 
        where (pr.ID_User == System.Web.HttpContext.Current.User.Identity.Name) 
        where (rg.RepGroup_ID == pr.ID_RepGroup) 
        order by rg.nOrder 
        select (rg.RepGroup_Name, rg.RepGroup_ID) 
        #> 
      res.Take(7).ToArray() 

這只是示例代碼,它在任何linq2sql,SqlAdapter Fill命令上都失敗。

下面是詳細信息:

System.InvalidOperationException was unhandled by user code 
     Message = There is a designated this command Command open DataReader, which requires the prior close. 
     Source = System.Data 
     StackTrace: 
      in System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute (SqlCommand command) 
      in System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute (String method, SqlCommand command) 
      in System.Data.SqlClient.SqlCommand.ValidateCommand (String method, Boolean async) 
      in System.Data.SqlClient.SqlCommand.RunExecuteReader (CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) 
      in System.Data.SqlClient.SqlCommand.RunExecuteReader (CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) 
      at System.Data.SqlClient.SqlCommand.ExecuteReader (CommandBehavior behavior, String method) 
      in System.Data.SqlClient.SqlCommand.ExecuteDbDataReader (CommandBehavior behavior) 
      in System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader (CommandBehavior behavior) 
      in System.Data.Common.DbDataAdapter.FillInternal (DataSet dataset, DataTable [] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) 
      in System.Data.Common.DbDataAdapter.Fill (DataTable [] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) 
      in System.Data.Common.DbDataAdapter.Fill (DataTable dataTable) 
      in Flow_WEB_Nemerle.SQLModule.GetReport (String Param1, Int32 Param2, Int32 Group, Int32 DayOfMonth, String ContactHour, Int32 MyCase, Boolean ForDia, Int32 step, Int32 maximumRows, Int32 startRowIndex) in D: \ SVN \ FlowServer \ trunk \ FS WEB \ Flow_WEB_Nemerle2 \ SQLReportModule.n: line 96 
     InnerException: 

我怎樣才能避免這種麻煩不爲它創建一個Windows服務/ WCF。我想要真正快速的解決方案...也許我需要動態類的方法?

+2

你的類不應該是靜態的,但方法應該是靜態的,如果你發佈類的方法,那麼我們可以幫助你更好。 – 2011-04-12 10:37:35

+0

爲什麼你有靜態類?將它們改爲實例類,你會沒事的。 – 2011-04-12 10:38:23

+0

@Waqas - 不,該方法不應該是靜態的,因爲它會導致非常相同的行爲。 – 2011-04-12 10:40:02

回答

3

Web服務器使用多個線程來處理請求。如果他們都使用相同的數據庫連接,那麼這對於網站來說是一個瓶頸。

您應該爲每個請求使用單獨的數據庫連接。這樣,線程之間就沒有併發問題,並且不會產生不必要的瓶頸。

+0

但是,如何指定我的連接應該不同? – Cynede 2011-04-12 10:44:25

+0

如果您使用具有相同連接字符串(即不是靜態)的每個實例連接,則ADO.NET連接池將處理到數據庫服務器的多個連接 – DaveRead 2011-04-12 10:46:51