繁忙的DataReader破坏了我的网站

时间:2011-04-12 10:30:49

标签: asp.net

所以...我有一些静态类,有一些方法可以将一些数据返回到我的数据表。

此方法的问题是多次访问。何时不同的客户端会话同时调用此方法 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,我该如何避免这种麻烦。我想要快速解决方案......也许我需要动态类和方法?

1 个答案:

答案 0 :(得分:2)

Web服务器使用多个线程来处理请求。如果他们都使用相同的数据库连接,那就是网站的瓶颈。

您应该为每个请求使用单独的数据库连接。这样,线程之间就没有并发问题,也没有创建一个不必要的瓶颈。

相关问题