Linq-To-Sql和MARS出现问题-当前命令发生了严重错误.结果(如有)应丢弃 [英] Linq-To-Sql and MARS woes - A severe error occurred on the current command. The results, if any, should be discarded

查看:127
本文介绍了Linq-To-Sql和MARS出现问题-当前命令发生了严重错误.结果(如有)应丢弃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们基于CodePlex上Kigg项目的设计建立了一个网站:

http://kigg.codeplex.com/releases/view/28200

基本上,代码使用存储库模式,并基于Linq-To-Sql进行存储库实现.完整的源代码可以在上面的链接中找到.

该网站已经运行了一段时间,大约一年前,我们开始出现以下错误:

  • 已经有与此命令关联的打开的DataReader,必须先关闭它.
  • ExecuteNonQuery需要打开且可用的连接.连接的当前状态为关闭.

这些是我根据内存可以找到的最接近的错误示例.当网站流量开始增加时,就会开始发生这些错误.在把头撞到墙上之后,我弄清了,假设问题是在Linq-To-Sql中继承的,以及我们如何使用同一连接在单个Web请求中调用多个命令.

通常,我发现了MARS(多个活动结果集),并将其添加到数据上下文的连接字符串中,就像魔术一样,我所有的错误都消失了.

现在,快进大约1年,站点访问量已大大增加.每个星期左右,我都会在SQL Server中收到一条错误消息:

当前命令发生了严重错误.结果(如果有的话)应该被丢弃

此错误发生后,我立即在错误日志中收到数百到数千个InvalidCastException错误.基本上,每次对Linq-To-Sql数据上下文的调用都会显示此错误.只有在重新启动Web服务器之后,这些错误才会清除.

我在Micosoft支持站点上阅读了一篇描述我的问题的文章(减去InvalidCastException错误),并指出解决方案是,如果我要使用MARS,那么我也应该使用Asncronous Processing = True.我试过了,但是也不能解决我的问题.

不太确定从这里去哪里.希望这里的人以前已经看到并解决了这个问题.

解决方案

因此,在进行大量重构和重新架构之后,我们发现问题一直是MARS(多个活动结果集)本身.不知道为什么或到底会发生什么,但MARS会以某种方式混淆结果集,并且直到重新启动Web应用程序后才能恢复.

我们删除了MARS,错误停止了.

如果我没记错的话,我们添加了MARS来解决使用LinqToSql已关闭连接/命令的问题,并尝试访问尚未加载的对象图.没有MARS,我们会得到一个错误.但是,当我们添加MARS时,似乎并不在乎.这确实是一个很好的例子,说明我们并不真正了解自己在做什么,因此我们从中学到了一些宝贵的经验.

希望这对其他经历过的人有所帮助.

感谢所有发表评论和答案的方式.

We have built a website based on the design of the Kigg project on CodePlex:

http://kigg.codeplex.com/releases/view/28200

Basically, the code uses the repository pattern, with a repository implementation based on Linq-To-Sql. Full source code can be found at the link above.

The site has been running for some time now and just about a year ago we started to get errors like:

  • 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.

These are the closest error examples I can find based on my memory. These errors started to occur when the site traffic started to pick up. After banging my head against the wall, I figured out assumed that the problem is inherit within Linq-To-Sql and how we are using the same connection to call multiple commands in a single web request.

Evenually, I discovered MARS (Multiple Active Result Sets) and added that to the data context's connection string and like magic, all of my errors went away.

Now, fast forward about 1 year and the site traffic has increased tremendously. Every week or so, I will get an error in SQL Server that reads:

A severe error occurred on the current command. The results, if any, should be discarded

Immediately after this error, I receive hundreds to thousands of InvalidCastException errors in the error logs. Basically, this error shows up for each and every call to the Linq-To-Sql data context. Only after I restart the web server do these errors clear up.

I read a post on the Micosoft Support site that descrived my problem (minus the InvalidCastException errors) and stating the solution is that if I'm going to use MARS that I should also use Asncronous Processing=True. I tried this, but it did not solve my problem either.

Not really sure where to go from here. Hopefully someone here has seen and solved this problem before.

解决方案

So after much refactoring and re-architecting, we figured out that problem all along is MARS (Multiple Active Result Sets) itself. Not sure why or what happens exactly but MARS somehow gets result sets mixed up and doesn't recover until the web app is restarted.

We removed MARS and the errors stopped.

If I remember correctly, we added MARS to solve the problem where a connection/command was already closed using LinqToSql and we tried to access an object graph that hadn't been loaded. Without MARS, we'd get an error. But when we added MARS, it seemed to not care about it. This is really a great example of us not really understanding what the heck we were doing and we learned some valuable (and expensive) lessons from this.

Hope this helps others who have experienced this.

Thanks to all how have contributed their comments and answers.

这篇关于Linq-To-Sql和MARS出现问题-当前命令发生了严重错误.结果(如有)应丢弃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆