事务隔离级别(IsolationLevel)
生活随笔
收集整理的這篇文章主要介紹了
事务隔离级别(IsolationLevel)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
來源:https://www.cnblogs.com/wms01/p/6183241.html
事務隔離級別(IsolationLevel)
事務的特性(ACID) 1、原子性(Atomicity)事物是數據庫的邏輯工作單位,事務中的諸多操作要么全做要么全不做 2、一致性(Consistency)事務執行結果必須是使數據庫從一個一致性狀態變到另一個一致性狀態 3、隔離性(Isolation)一個數據的執行不能被其他事務干擾 4、持續性/永久性(Durability)一個事務一旦提交,它對數據庫中的數據改變是永久性的隔離級別與并發性是互為矛盾的:隔離程度越高,數據庫的并發性越差;隔離程度越低,數據庫的并發性越好,這點很好理解
事務的隔離級別(IsolationLevel)
1 // 摘要: 2 // Specifies the transaction locking behavior for the connection.3 public enum IsolationLevel4 {5 // 摘要: 6 // A different isolation level than the one specified is being used, but the7 // 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.? )
隔離級別與讀取現象
| 未授權讀取/未提交讀?? read uncommitted | 可能發生 | 可能發生 | 可能發生 |
| 授權讀取/提交讀????????? read committed | - | 可能發生 | 可能發生 |
| 重復讀?????? ????????????????? read repeatable | - | - | 可能發生 |
| 序列化???????????????? ?????? serializable???????? | - | - | - |
常見數據庫的默認級別
| 數據庫 | 默認隔離級別 |
| Oracle | read committed |
| SqlServer | read committed |
| MySQL(InnoDB) | Read-Repeatable |
?
總結
以上是生活随笔為你收集整理的事务隔离级别(IsolationLevel)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring源码:spring-tx 事
- 下一篇: Zeppelin原理简介