事务隔离级别(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 2016-12-15 14:49  Jade_K  阅读(6047)  评论(1编辑  收藏

导航