事务隔离级别(IsolationLevel)

事务的特性(ACID)
1、原子性(Atomicity)
  事物是数据库的逻辑工作单位,事务中的诸多操作要么全做要么全不做
2、一致性(Consistency)
  事务执行结果必须是使数据库从一个一致性状态变到另一个一致性状态
3、隔离性(Isolation)
  一个数据的执行不能被其他事务干扰
4、持续性/永久性(Durability)
  一个事务一旦提交,它对数据库中的数据改变是永久性的

隔离级别与并发性是互为矛盾的:隔离程度越高,数据库的并发性越差;隔离程度越低,数据库的并发性越好,这点很好理解

事务的隔离级别(IsolationLevel)

复制代码
 1     // 摘要: 
 2     //     Specifies the transaction locking behavior for the connection.
 3     public enum IsolationLevel
 4     {
 5         // 摘要: 
 6         //     A different isolation level than the one specified is being used, but the
 7         //     level cannot be determined.
 8         Unspecified = -1,
 9         //
10         // 摘要: 
11         //     The pending changes from more highly isolated transactions cannot be overwritten.
12         Chaos = 16,
13         //
14         // 摘要: 
15         //     A dirty read is possible, meaning that no shared locks are issued and no
16         //     exclusive locks are honored.
17         ReadUncommitted = 256,
18         //
19         // 摘要: 
20         //     Shared locks are held while the data is being read to avoid dirty reads,
21         //     but the data can be changed before the end of the transaction, resulting
22         //     in non-repeatable reads or phantom data.
23         ReadCommitted = 4096,
24         //
25         // 摘要: 
26         //     Locks are placed on all data that is used in a query, preventing other users
27         //     from updating the data. Prevents non-repeatable reads but phantom rows are
28         //     still possible.
29         RepeatableRead = 65536,
30         //
31         // 摘要: 
32         //     A range lock is placed on the System.Data.DataSet, preventing other users
33         //     from updating or inserting rows into the dataset until the transaction is
34         //     complete.
35         Serializable = 1048576,
36         //
37         // 摘要: 
38         //     Reduces blocking by storing a version of data that one application can read
39         //     while another is modifying the same data. Indicates that from one transaction
40         //     you cannot see changes made in other transactions, even if you requery.
41         Snapshot = 16777216,
42     }
复制代码

常用状态分析:

  1、ReadUncommitted

    表示:未提交读。当事务A更新某条数据的时候,不容许其他事务来更新该数据,但可以进行读取操作

  2、ReadCommitted

    表示:提交读。当事务A更新数据时,不容许其他事务进行任何的操作包括读取,但事务A读取时,其他事务可以进行读取、更新

  3、RepeatableRead

    表示:重复读。当事务A更新数据时,不容许其他事务进行任何的操作,但是当事务A进行读取的时候,其他事务只能读取,不能更新

  4、Serializable

    表示:序列化。最严格的隔离级别,当然并发性也是最差的,事务必须依次进行。

 读取现象

 通过一些现象,可以反映出隔离级别的效果。这些现象有:

  • 更新丢失(lost update):当系统允许两个事务同时更新同一数据时,发生更新丢失。
  • 脏读(dirty read):当一个事务读取另一个事务尚未提交的修改时,产生脏读。
  • 不重复读(nonrepeatable read):同一查询在同一事务中多次进行,由于其他提交事务所做的修改或删除,每次返回不同的结果集,此时发生非重复读。(A transaction rereads data it has previously read and finds that another committed transaction has modified or deleted the data.  )
  • 幻读(phantom read):同一查询在同一事务中多次进行,由于其他提交事务所做的插入操作,每次返回不同的结果集,此时发生幻像读。(A transaction reexecutes a query returning a set of rows that satisfies a search condition and finds that another committed transaction has inserted additional rows that satisfy the condition.  )

隔离级别与读取现象

隔离级别脏读   Dirty Read 不可重复读取  NonRepeatable Read 幻读 Phantom Read 
未授权读取/未提交读   read uncommitted 可能发生 可能发生 可能发生
授权读取/提交读          read committed - 可能发生 可能发生
重复读                         read repeatable - - 可能发生
序列化                        serializable         - - -

常见数据库的默认级别

数据库 默认隔离级别
Oracle read committed
SqlServer read committed
MySQL(InnoDB) Read-Repeatable

 

posted on   Jade_K  阅读(8728)  评论(1编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示