關於交易隔離等級

來源:互聯網
上載者:User

標籤:style   blog   資料   width   io   for   

資料庫事務常見的三種現象:

1 髒讀(dirty read):未提交讀。(A修改年齡為20,B讀出20後,A復原為18。)

   出現情境:大多數資料庫預設的層級為提交讀(SQLServer,Orcal),所以除非設定隔離等級為Read uncommitted,否則不會出現。

   解決方案:SET TRANSACTION ISOLATION LEVEL read committed

2 不可重複讀取:

  出現情境:

  執行事務A後,立馬執行事物B,會出現事務A中出現的兩次查詢值不同。

--事務Abegin tran select * from  [HM_Student] where id=8waitfor delay ‘00:00:05‘select * from  [HM_Student] where id=8commit tran --事務Bbegin tran --delete [HM_Student]  where id=6update [HM_Student] set age=333352 where id=8waitfor delay ‘00:00:1‘commit tran 

  解決方案:在事務前加:SET TRANSACTION ISOLATION LEVEL repeatable committed

3 幻讀:與第2個相似,是一個事物中多次查詢,其他事物對資料進行了刪或者增而出現的情況。

  解決方案:SET TRANSACTION ISOLATION LEVEL serializable

 

總結:

隔離等級

髒讀

不可重複讀取

幻像

說明

未提交讀(read uncommitted)

如果其他事務更新,不管是否提交,立即執行

提交讀(read committed預設)

讀取提交過的資料。如果其他事務更新沒提交,則等待

可重複讀(repeatable read)

查詢期間,不允許其他事務update

可串列讀(serializable)

查詢期間,不允許其他事務insert或delete

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.