sql ef datacontext muti thread problem

來源:互聯網
上載者:User

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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.