正如先前的Stack Overflow问题(TransactionScope和连接池以及SqlConnection如何管理IsolationLevel?)所证明的那样,事务隔离级别在SQL Server和ADO.NET(也是System.Transactions和EF)的池化连接中泄漏,因为它们建立在ADO.NET).
这意味着,在任何应用程序中都可能发生以下危险事件序列:
问题:防止这种情况的最佳方法是什么?现在到处都需要使用显式交易吗?
这是一个独立的复制品.您将看到第三个查询将继承第二个查询中的Serializable级别.
class Program
{
static void Main(string[] args)
{
RunTest(null);
RunTest(IsolationLevel.Serializable);
RunTest(null);
Console.ReadKey();
}
static void RunTest(IsolationLevel? isolationLevel)
{
using (var tran = isolationLevel == null ? null : new TransactionScope(0, new TransactionOptions() { IsolationLevel = isolationLevel.Value }))
using (var conn = new SqlConnection("Data Source=(local); Integrated Security=true; Initial Catalog=master;"))
{
conn.Open();
var cmd = new SqlCommand(@"
select
case transaction_isolation_level
WHEN 0 THEN 'Unspecified'
WHEN 1 THEN …
Run Code Online (Sandbox Code Playgroud) 从我正在阅读的内容来看,为了在.NET中使用TransactionScope,您需要在Windows中运行分布式事务处理协调器服务.我关闭了该服务,我的应用程序似乎运行相同,回滚交易没问题.
我错过了什么吗?它是如何工作的?我正在运行Windows 7并在VisualStudio 2010上运行webapp.