Linq-To-Sql and MARS woes - A severe error occurred on the current command. The results, if any, should be discarded - sql-server

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.

I have the same issue. Once the errors start, I have to restart the IIS Application Pool to fix.
I have not been able to reproduce the bug in dev despite trying many different scenarios involving multi-threading, leaving connections open, etc etc.
One possible lead I do have is that amongst the errors in the server Event Log is an OutOfMemoryException for the Application Pool. Perhaps this is the underlying cause of the spurious SQL Datareader errors (a memory leak elsewhere). Although again I haven't been able to reproduce this in dev.
Obviously if you are using a 64 bit OS then this is probably not the cause in your case.

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.

I understand you figured out the solution..
Following is not a direct solution to the problem; but it is good for others to take a look at
What does "A severe error occurred on the current command. The results, if any, should be discarded." SQL Azure error mean?
http://social.msdn.microsoft.com/Forums/en-US/bbe589f8-e0eb-402e-b374-dbc74a089afc/severe-error-in-current-command-during-datareaderread

Related

How to debug the cause for SQLiteError(11): database corruption

I have been tasked with finding the cause for 2 repeated lines we find in about 5% our logs in our Beta environment (this app is not in prod yet). They are as follow:
SQLite error (11): database corruption at line 55472 of [cf538e2783]
SQLite error (11): database corruption at line 55514 of [cf538e2783]
That's it. No other info is available, and it's always those two lines. They are being logged by System.Data.SQLiteLog.LogEventHandler and the Log System is Trace.
Interestingly, it appears that the database continues to function and usage of our application continues without any apparent problems.
So my question is: Is there actually an issue, and, if so, how do you go about identifying it?
In my research on the issue I have to believe that there is a legitimate problem here. According to www.sqlite.org corruption in SQLite should be rather rare. A false corruption report can be caused due to database shrinkage (8.1), but I don't think that's the case here. I must assume at this point that the report is correct and the database is actually corrupt.
However, how could the database continue to function normally for hours of use if it is, in fact, corrupted?
Many sites give solutions on how to fix this issue (by reverting to a backup, or otherwise recreating the database), but I need to find what's causing this issue.
I have tried installing DB Browser for SQLite and browsing the database, but all seems well and there doesn't seem to be a way to "browse" to the lines that are reportedly corrupt. (There must be a way to do so, though, right? Why would they give line numbers if it's impossible to know what those line numbers are?)
Are there operations or areas that are likely culprits in this circumstance that I can investigate? How do I identify the lines that are being referenced in the error messages?

MS Access randomly(?) has problems opening my report

I have been working with MS Access for some days now, and I have noticed that sometimes errors appear on certain actions (mostly apparently pertaining to VBA code), while I really have not changed anything.
The most annoying instance of this problem is the following example:
This was an existing DB, and I created a new "smarter" report. When I go through the forms, eventually generating my report, about half the time there is no problem, and my fancy new report appears at the end without any problems
Yet other times I get "Run-time Error '2427': You entered an expression that has no value", and my report does not open. As far as I can see, this occurs entirely at random. I did not change any code or query's, it can even happen when just closing the report, and then trying to directly open it again.
Is this just some weird bug in Access or something?
I had an issue like yours not too long ago. Being a flat file system ms access is fragile, and I would recommend doing a compact/repair to your file when closed (not just set compact on close). Good luck.

SQLException Obtaining more information

Currently working with converting SQLException error messages into messages that are more useful for the end user. My largest issue has been finding the information I'm looking for.
For instance, error code 8114 is
"Error converting data type [Type1] to [Type2]"
And error code 8152 is
"String or binary data would be truncated"
Using ADO.NET in VB.NET with SQLServer, is there a simple way to figure out which columns are affected by these errors?
The problem is that the error from SQL Server does not give that information so the only way to identify which field is causing the issue is to break apart your SQL statement to run it for the separate fields individually. That sounds like a lot of work for something the developer should be testing to make sure that it does not happen to begin with. There are errors that are not the developer's fault, but the two you listed here are things the developer should be keeping from happening and should be part of the unit tests for the process.
Some of the errors I can think of that are not a developer error, are ones to do with connection errors with SQL Server or corruption errors with the database.
I actually like the fact that my user's can't always figure out what is causing the problem because it tends to force them to tell me there is a problem with my program. If they figure out how to get around an error that I should have fixed, they tend to not tell me there is a problem and sometimes it has been years before I found out there was an issue.

Debugging Silverlight RIA Services SubmitChanges

I'm having huge difficulty debugging database operations from Silverlight RIA. This is understandable, I suppose, since database operations are abstracted by about 3 layers of services and ORM and stuff, but is there any way I can see what the database is telling me regarding the issue? I'm able to find an EntityConflict object which doesn't offer much information.
The only field which seems to indicate any problem is "IsDeleted" property equal to true, however this property is not well documented at MSDN and I cannot even be sure that having it be true is an issue.
I've attempted to use fiddler, however no errors are reaching that, I've attached to the application's unhadled exception, but that points me back to EntityConflict, which I am recovering through handling SubmittedChanges event and accessing the args. I've even enabled WCF tracing to attempt to recover some information but, of course, nothing there either.
Did you inspect SubmitOperation.Error after the submit operation failed? It should contain the error information you seek. More information on error handling can be found here: Link
The issue was a known issue, at least a few years ago, where INSTEAD OF INSERT doesn't return Scope_identity for a column inserted by this method. EF uses this value to verify that an insert was completed. When it fails, you get a deletion conflict. This is, apparently, a known issue with the SQL Server EF provider, however I have been unable to confirm that it's been resolved.

Visual Studio 2008 ContextSwitchDeadlock with log4net and NHibernate

I'm facing an extremely weird bug here and I'm not really sure If I'm following the right path to solving it or even how to solve it.
Here is the problem I'm facing: I start debugging a WPF application which uses log4net, NHibernate and LINQ to NHibernate, and when I try to get an Entity from the database my application and sometimes VS hang for a lot of time, and after a while an exception dialog opens showing a message containing the following information on a ContextSwitchDeadlock MDA:
The CLR has been unable to transition from COM context 0x34fc1a0 to COM context 0x34fc258 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this
I copied the code files to a new project and deleted the old project to see If I could make this message disappear, thinking it had something to do with my configuration. I started adding few things at a time to see what was causing it, and when I included log4net configuration code the bug appeared again. First I included it through AssemblyInfo and later trough code configuration on application startup, and absolutely nothing changed at all :(
So, here are my findings:
It only happens when I'm using log4net.
It happens when NHibernate loads an Entity from database (lazy loading).
I don't know what might be the source of this bug. It only happens when debugging in Visual Studio. I've tried following the steps on the "Enabling and Disabling MDAs" section of the following page: http://msdn.microsoft.com/en-us/library/d21c150d.aspx, but that doesn't work either, VS still hangs and it's memory usage increases.
When I run the program normally none of this happens, so I'm pretty sure this is not a deadlock situation, as this question suggests: contextswitchdeadlock (I've also tried the solutions posted there).
Because of that, I've decided to disable log4net and enable it again when deploying my app.
I'm posting this question to find out if somebody else has faced this bug or if somebody has some suggestions on how to solve it. Finally, it might help somebody else facing this very same problem.
When using the DebugAppender all the entities in the databases are loaded and all its data written to the debug output. That was causing the ContextSwitchDeadlock MDA since it tookmore than 60 seconds to run.
Disabling the DebugAppender solved my problem.
Thanks to Mauricio Scheffer for the tip.

Resources