sql ef datacontext muti thread problem

http://www.sqlservercentral.com/Forums/Topic728749-149-1.aspx#bm802921

 

 

Hi,

Recently I've started getting the following severity 20 error quite often on my SQL 2005 box:
The server will drop the connection, because the client driver has sent multiple requests while the session is in single-user mode. This error occurs when a client sends a request to reset the connection while there are batches still running in the session, or when the client sends a request while the session is resetting a connection. Please contact the client driver vendor.

Currently I'm getting around 30 of these a day, so unfortunately it's not a single occurrence. We have a number of relatively high load websites in front of the database, all of them running ASP.NET 3.5 accessing the database through LINQ to SQL and SqlCommands directly. We do not use MARS explicitly, though I suspect that DataContext might do so internally. I have neither explicitly enabled nor disabled MARS in the connection string, so I'd assume it would default to not being enabled. I'm preparing to push out an update that explicitly disables MARS so I can rule that out. All my Googling yielded rather useless results, only referencing MARS as a possible reason.

I've also had a couple of cases where my ASP.NET apps broke down whenever they tried to contact the database with the following error:
"ExecuteReader requires an open and available Connection. The connetion's current state: Broken"

So apparently a connection had broken (possible due to the error received on the DB end), even though it was still available in the connection pool. Clearing the connection pool solved this issue. I've only seen this error once, so it may not be correlated - it did happen after I started receiving the DB error however.

The errors seem to occur more often during daytime/highest load, so it's definitely affected by load levels.

Besides disabling MARS and seeing if it has a result, I've considered setting up a serverside trace and let it run till I get one of the errors and see if I can trace down the affected process and hopefully see the sequence leading up to the error. Any suggestions as to how I may track down why this is happening?

A couple of counters to give you an idea of the load/box:
Databases: ~170
Transactions/Sec: ~800-1300
Logical connections: ~600
User connections: ~450-500
Total memory: ~6,5GB
Page life expectancy: ~23k

TCPv4 (as the Google results also suggest this might be a network issue):
Connection failures: ~47k
Connections active: ~59k
Connections established: ~500
Connections passive: 550k
Connections reset: ~16k
Seg/sec: ~1000-1500
Seg retransmitted/sec: ~10


Mark S. Rasmussen
Blog: improve.dk
Twitter: @improvedk

Internals geek & author of OrcaMDF

 

 

I ended up fixing this the hard way that I'd hoped to avoid.

I was storing my DataContext in HttpContext.Current.Items. Since I did not do any explicit threading during my request, there shouldn't be any concurrent accesses to the same DataContext. Although IIS may switch thread during a request, it still should not cause concurrent usage of the DC.

While I have not found out how, the problem vanished as soon as I started instantiating my DC's as needed and not sharing it in any way during the request. Somehow multiple threads must have used the same DC and thereby receiving and sending errant messages to/from the SQL Server.

你可能感兴趣的:(context)