Database Transaction Level

Source: Internet
Author: User
1. readuncommitted (dirty read) (read data being committed) readuncommitted (also known as reading uncommitted content) allows tasks to read uncommitted data changes in the database. Test Script: Create a table CREATETABLE [dbo]. [testTb] ([ID] [int] NULL, [Name] [char] (20) NULL) 2. create transaction A: insert data begin

1. read uncommitted (dirty read) (read data being committed) read uncommitted (also known as read uncommitted content) allows tasks to read uncommitted data changes in the database. Test Script: create table [dbo]. [testTb] ([ID] [int] NULL, [Name] [char] (20) NULL) 2. create transaction A: insert data begin

1. read uncommitted (dirty read) (read data being committed)

Read uncommitted (also known as reading uncommitted content) allows tasks to read uncommitted data changes in the database.

Test script:

  1. Create a table

Create table [dbo]. [testTb] (
[ID] [int] NULL,
[Name] [char] (20) NULL
)

2. Create transaction A: insert data

Begin tran
Insert into testTb values (5, 'E ')

Waitfor delay '00: 00: 10'

Commit;

3. Create transaction B: Read data

Set transaction isolation level read uncommitted
Begin tran

Select * from testTb


Commit;

4. Run transaction A to run transaction B immediately. At this time, transaction A has not been committed, and transaction B can read the data being committed by transaction.

2. read committed (submit read) (cannot read data being committed)

Level Read Committed (also known as reading submitted content) can prevent dirty reads. This level of query reads only submitted data changes. If the transaction needs to read data modified by another unfinished transaction, the transaction will wait until the first transaction is completed (committed or rolled back ).

Create table [dbo]. [testTb] (
[ID] [int] NULL,
[Name] [char] (20) NULL
)

2. Create transaction A: insert data

Begin tran
Insert into testTb values (5, 'E ')

Waitfor delay '00: 00: 10'

Commit;

3. Create transaction B: Read data

Set transaction isolation level read committed
Begin tran

Select * from testTb
Commit

4. Run transaction A to run transaction B immediately. At this time, transaction A has not been committed, and transaction B must wait until transaction A completes before it can read data.

3. repeatable read (repeatable read) (READ data cannot be modified)

The specified statement cannot read the rows modified but not committed by other transactions. It is specified that no other transaction can modify the data read by the current transaction before the current transaction is completed.

Create table [dbo]. [testTb] (
[ID] [int] NULL,
[Name] [char] (20) NULL
)

2. Create transaction A: update data

Begin tran
Update testTb set Name = 'cv 'where ID = '8'

Waitfor delay '00: 00: 10'

Commit;

3. Create transaction B: Read data

Set transaction isolation level Repeatable read

Begin tran

Select * from testTb
Commit

4. Run transaction A to run transaction B immediately. At this time, transaction A has not been committed, and transaction B must wait until transaction A completes before it can read data.

4. SERIALIZABLE (sequential read) (Reading data cannot be inserted or modified)

  • The statement cannot read data modified by other transactions but not committed.
  • No other transaction can modify the data read by the current transaction before the current transaction is completed.
  • Before the current transaction is completed, other transactions cannot use the key value read by any statement in the current transaction to Insert a new row.

Create table [dbo]. [testTb] (
[ID] [int] NULL,
[Name] [char] (20) NULL
)

2. Create transaction A: update data and insert data

Begin tran
Insert into testTb values (9, 'D ')

Update testTb set Name = 'cv 'where ID = '8'

Waitfor delay '00: 00: 0'

Commit;

3. Create transaction B: Read data

Set transaction isolation level serializable
Begin tran

Select * from testTb


Commit;

4. Run transaction A to run transaction B immediately. At this time, transaction A has not been committed, and transaction B must wait until transaction A completes before it can read data.

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.