Problems with Linq-To-Sql and MARS. A serious error has occurred in the current command. Results, if any, should be discarded.

We have created a website based on the Kigg project on CodePlex:

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

Basically, the code uses the repository template, with the implementation of the repository based on Linq-To-Sql. The full source code can be found at the link above.

The site has been working for some time, and about a year ago we started getting errors, for example:

  • There is already an open DataReader associated with this Command, which should be closed first.
  • ExecuteNonQuery requires an open and accessible connection. The status of the current connection is closed.

These are the closest examples of errors that I can find based on my memory. These errors began to occur when site traffic began. After I hit my head against the wall, I realized that the problem is inherited in Linq-To-Sql and how we use the same connection to invoke several commands in the same web request. p>

As a rule, I discovered MARS (several active result sets) and added this to the data context connection string and, like magic, all my errors went away.

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

A serious error occurred in the current team. Results, if any, should be discarded.

Immediately after this error, I get from hundreds to thousands of InvalidCastException errors in the error logs. Basically, this error appears for every Linq-To-Sql data context call. Only after restarting the web server are these errors resolved.

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

Not quite sure where to go from here. I hope someone here has seen and solved this problem before.

0
sql-server linq-to-sql mars kigg
Feb 19 '13 at 21:43
source share
3 answers

So, after a lot of refactoring and restructuring, we found out that this problem is MARS (many active result sets). It's unclear why and what exactly is happening, but MARS somehow mixes the result sets and does not recover until the web application restarts.

We removed MARS and the errors stopped.

If I remember correctly, we added MARS to solve the problem when the connection / command was already closed using LinqToSql, and we tried to access the graph of the object that was not loaded. Without MARS, we would get an error. But when we added MARS, it didn't seem to care about that. This is a really great example of the fact that we do not understand what we are doing, and we have learned valuable (and expensive) lessons from this.

Hope this helps others who have experienced this.

Thanks to everyone who contributed their comments and answers.

+1
May 25 '13 at 21:09
source share

I have the same problem. As soon as the errors start, I have to restart the IIS application pool to fix it.

I could not reproduce the error in dev, despite many different scenarios related to multithreaded access, opening connections, etc. etc.

One of the possible options that I have is that among the errors in the Event Log server is an OutOfMemoryException for the application pool. Perhaps this is the main cause of false SQL Datareader errors (memory leak elsewhere). Although I have not been able to reproduce this in dev yet.

Obviously, if you are using a 64-bit OS, this is probably not the reason in your case.

+1
May 20, '13 at 3:37
source share

I understand that you have figured out a solution.

The following is not a direct solution to the problem; but good for others to look at

0
Dec 10 '13 at 16:05
source share



All Articles