LINQ迸发访问错误提示:DataReader associated with this Command which must be closed first

我有一个应用是采用LINQ生成的DBML写的,同时执行两种操作的时候出现迸发错误。

操作一:读表

操作二:写表(时间较长的操作,大约持续1分钟)

 

在web.config中增加下面的参数可以消除这种错误。

connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=SSPI;MultipleActiveResultSets=true"

 

我照这样做了,写表的同时,读表不出错了。可是写表的操作却开始一直报错,看来还是没有解决问题。还有一种解释:


I posted this question on the AZGroups mailing list and got a number of excellent responses. Basically my problem is that I shouldn't be using a singleton pattern for the DataContext but should be instantiating a new DataContext for each job unit that's performed.

 

我注意到我的代码的写法,可能是这里出错了,为了不是每次表操作都去建立一个新的DBDataContext实例,我就统一建立了一个静态实力,看上面的说法,这样不行。看来需要给每一个操作定义一个单独的实例。 那看来得每个操作页面定义一个对象实例,操作完成以后再关掉,你有什么好的办法么?

 

这样子不行:  public static MyDBDataContext db = new myDBDataContext();

 

这篇里面写了一些讨论,好像还没有结果呀,我等下仔细看看。

http://weblogs.asp.net/bigyanr/default.aspx

 

==============================

问题描述原文链接,

http://guyellisrocks.com/sql-server/linq-to-sql-architecture-question/

 

LINQ to SQL architecture question

I have structured a project with a single LINQ to SQL .dbml file and a single DataAccess class that is used to call stored procs and query against this this DBML class. The DataAccess class implements the singleton pattern inasmuch as their's a static DataAccess property which is used for all DB access.

The business layer of the application creates an instance of the DataAccess object, calls the appropriate data access function and then returns.

When running as a web application it is being hit rapidly by 2 clients: (1) a browser calls to it about 1 to 50 times a second (writes data to DB) and (2) an excel spreadsheet calls the web app via a web service on an ad hoc basis and does a query for data.

When the spreadsheet does the query (the browser's hitting the site up to 50 times a second at the same time) I am getting several errors such as the following:

  •     Invalid attempt to call MetaData when reader is closed
  •     There is already an open DataReader associated with this Command which must be closed first.
  •     ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.
  •     A transport-level error has occurred when receiving results from the server. (provider: Session Provider, error: 18 - Connection has been closed by peer)
  •     A severe error occurred on the current command.  The results, if any, should be discarded.
  •     A transport-level error has occurred when receiving results from the server. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)

The first 2 errors were solved by adding MARS to the connection string: MultipleActiveResultSets=true

The rest of them I solved by adding a static lock object to the DataAccess class and wrapping all calls to the database in a lock(lockObj) {}.

This solution doesn't feel right though.

Comments on the general approach I've taken... Comments on the lock() solution...

Filed under: SQL Server

 


 

你可能感兴趣的:(command)