ADO.NET 隔离级别 v TransactionScope 隔离级别 [英] ADO.NET Isolation Level v TransactionScope isolation level

查看:49
本文介绍了ADO.NET 隔离级别 v TransactionScope 隔离级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读到 ADO.NET 的默认隔离级别(当不使用事务时,即每个语句作为原子操作执行时)是READ COMMITTED"和 TransactionScope 的默认隔离级别(http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx) 是SERIALIZABLE".这有什么原因吗?

I have read that the default isolation level for ADO.NET (when transactions aren't used i.e. each statement is executed as an atomic operation) is 'READ COMMITTED' and the default isolation level for TransactionScope (http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx) is 'SERIALIZABLE'. Is there a reason for this?

我已经阅读了很多关于这个主题的网页,但我还没有找到我的具体问题的答案.

I have read many webpages on this subject, but I have not yet found an answer to my specific question.

推荐答案

原因是事务范围可以跨越多个(子)事务,所有事务都与分布式事务协调器 (MS-DTC) 协调.大多数情况下,您需要确保所有子事务都使用相同的数据,即使它们之间发生了提交.

The reason is that a Transaction scope can span multiple (sub)transactions that all are coordinated with a distributed transaction coordinator (MS-DTC). Most of the times you'll want to be sure that all subtransctions are working with the same data, even if a commit happened between them.

例如一个有 3 个方法的服务:checkbalance、increasebalance &减少余额.每个方法都会启动一个事务,打开一个数据库连接,执行一条 SQL,关闭连接并提交事务.

For example a service that has 3 methods: checkbalance, increasebalance & decreasebalance. Each method starts a transaction, opens a database connections, executes a SQL, closes the connection and commits the transaction.

一个典型的场景是此服务的客户端:

A typical scenario would then be for a client of this service to:

  1. 启动主事务
  2. 查看账户 1 的余额
  3. 减少帐户 1 的余额
  4. 增加帐户 2 的余额
  5. 提交主事务

IsolationLevel.Serializable 将确保所有服务调用 a) 从相同的数据开始,b) 在主事务运行时不允许其他人更改该数据.

IsolationLevel.Serializable will make sure that all service calls will a) start from the same data and b) no one else is allowed to alter that data while the master transaction is running.

IsolationLevel.ReadCommitted 可能导致账户 1 的余额低于零,如果其他人减少了足够的余额并在支票和减少之间提交了该交易.

IsolationLevel.ReadCommitted can cause the balance of account 1 to go below zero if someone else decreased the balance with a sufficient amount and committed that transaction between the check and the decrease.

这篇关于ADO.NET 隔离级别 v TransactionScope 隔离级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆