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

 


 

转载于:https://www.cnblogs.com/xblues/archive/2008/08/01/1258311.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值