What is the difference between five transaction isolation in SQL Server

Source: Internet
Author: User
Tags commit execution rollback

The garden has a very good article about SQL Server transaction isolation, feeling a lot of the introduction from the concept, for those beginners, see the touch of understanding is profound, so no longer repeat, the emphasis is on the example demonstration above.

The first explanation is what transaction isolation is, and the isolation level of a transaction controls how it affects other transactions and is affected by other firms.

1. READ UNCOMMITTED, which results in dirty reads (which can read changes not committed by other transactions) and non-repeatable reads (data read by the transaction is modified by another transaction, inconsistent when read again)

Class

CREATE TABLE tranlevel (k int IDENTITY (1,1), Val int)
    
insert into Tranlevel (val) VALUES (1)
    
inserts into Tranlevel (v AL) VALUES (2)
    
INSERT into Tranlevel (val) VALUES (3)

Execute Query1 First, and then create a new query to execute immediately Query2

Query1:

BEGIN TRAN Query1
    
--Modify
    
UPDATE tranlevel SET val = 9 in a transaction-  ' Wait 10 seconds while transaction 2 runs '
    
WAITFOR DELAY ' 00:00:10 '
    
--Do not commit changes, ROLLBACK TRANSACTION
    
ROLLBACK TRAN Query1

Query2:

--Sets the current session transaction isolation level for uncommitted read
    
set TRANSACTION isolation levels READ UNCOMMITTED
    
begin TRAN Query2
    
SELECT ' transaction 2 start concurrent execution, Read to Transaction 1 modified but not submitted data, is dirty read '
    
select * from Tranlevel
    
select ' Transaction 2 wait 10 seconds, let transaction 1 Finish '
    
WAITFOR DELAY ' 00:00:10
    
' The result of the select ' two reads is inconsistent and is not repeatable read '
    
select * from Tranlevel
    
COMMIT TRAN Query2

Here's a look at the results of Query2 execution:

The result is obvious, if the transaction isolation level is set to uncommitted read, it can cause dirty read and non repeatable reads, the least restrictive of these transaction isolation levels, and the smallest resource allocated by SQL Server.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.