High volume site using ADO.NET TransactionScope vs ExecuteCommand on NOLOCK, READ UNCOMMITTED directly?

做~自己de王妃 提交于 2019-12-03 00:42:32

First of all please avoid uncommitted reads, they can cause lots of issues. A much better approach is just to set the database to snapshot isolation. This is what Jeff did.

Jeff basically said: "bla bla bla, be real, bla bla bla, database theoreticians, bla bla bla, READ UNCOMMITTED can be useful for REAL production apps that don't need data consistency." Jeff is not a DBA, fortunately there are many DBAs out here on SO.

The problem with Omar's approach is that it can leak connections with "read uncommitted" isolation level in to your connections pool which could wreak havoc in your website. Meaning random statement may be executed in read uncommitted.

Javed approach would be much better because on dispose MS have the chance to clean stuff up on the connection.

EDIT If you are having performance issues with Javed's approach you could look at rolling your own transaction manager.

Some things you probably want to do:

  • Hold a stack of current transactions
  • Confirm you are on the creator thread when a transaction is committed
  • Reset the transaction isolation to its previous state on dispose
  • Rollback on dispose if the transaction was not committed.
  • Support nested rollbacks.

I'm a developer on a tools team in the SQL Server group at Microsoft. Many applications are not super-sensitive to transaction consistency, especially if you writing an app which does reporting or something where occasionally inconsistent data is not the end of the world. Of course, if you writing a financial application or something else which has very low tolerance for data inconsistency, you probably want to explore other solutions.

If do choose to use uncommitted reads, I have blogged a handy solution using extension methods in C#.

{My (poor) reputation prevents me from posting comments so I put this as an answer}

If you use IsolationLevel via System.Transactions and create a new Linq context within the transaction block, SQL Server ends up trying to call DTC to coordinate the transaction. That just happened to me and was quite unexpected.

Regarding transactions in .Net and the (somehow surprising) side-effect of the DTC, this document Introducing System.Transactions in the .NET Framework 2.0 by Juval Lowy explains things very well and is still fully valid (.Net4). Worth reading. (I would also have posting a comment... if I could.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!