Database Learning (Four) - Transaction Processing (dirty reads, non-repeatable read, phantom read this all in a)

Database Learning (Four) - Transactions

@

I. Introduction to transactions

1. Normal commit, automatic submission
commit;
2.rollback
delete from xxx where xx;
delete from xxx where xx;
savepoint sp1;
delete from xxx where xx;
rollback to sp1;
3. ACID properties of the transaction
A:原子性,操作集合不可分割
C:一致性,经过N个操作,数据状态不会改变
I:隔离性,隔离性会导致效率降低,为了提高程序效率,可以设置隔离级别
隔离级别:读未提交,读已提交,可重复度,序列化
数据不一致问题:脏读、不可重复读、幻读
D:持久性,所有数据修改持久化到介质中,不会因为程序关闭,导致丢失
4. characteristics of a transaction, which is the most critical?

All features are designed to ensure consistency, consistency is the ultimate pursuit.

Consistency is achieved by atom, isolation, durability guaranteed.

The locking mechanism:

In order to address concurrent access, data inconsistencies, have to consider locking granularity,

Lock granularity, the higher the efficiency, the larger the particle size, the lower the efficiency, mostly row-level locks

Second, dirty reads, phantom read, non-repeatable read test

1. Open a command line
select @@autocommit
set autocommit=0
2. Data Preparation
create database tran;
use tran;
create table psn(id int primary key,name varchar(10)) engine=innodb;
insert into psn values(1,"zhangsan");
insert into psn values(2,"lisi");
insert into psn values(3,"wangwu");
commit;
3. Testing Services
--事务包含四个隔离级别:(从上往下隔离级别越来越高
read uncommitted; --读未提交
read commited; --读已提交
repeatable read; --可重复读
(seriable) --序列化执行,串行
4. Test 1: generating an analog read dirty read uncommitted
set session transaction isolation level read uncommitted;
A:start transaction;
B:start transaction;
A:select * from psn;
B:select * from psn;
A:update psn set name='msb'
--读取结果为msb,产生脏读,因为A事务没有commit

A:commit
B:select * from psn;
--此时读取的数据是msb,正常读取

At this time, we have two tables for comparison and found inconsistent data, generate dirty read phenomenon

5. Test 2: non-repeatable read analog
set session transaction isolation level read committed;
A:start transaction;
B:start transaction;
A:update psn set name='ppp'
B:select * from psn;
A:commit;
B:select * from psn;

At this time carried out by AB inquiry found no dirty reads, but the emergence of non-repeatable read, because in a single transaction, B read out two different data

6. Test 3: simulated phantom read
set session transaction isolation level repeatable read;
A:start transaction;
B:start transaction;
A:update psn set name='lisi'
B:select * from psn;
A:commit;
B:select * from psn;

This is the climate has been submitted, but B hallucinations, or prior to the data, so called phantom reads.

7. Summary Table
Isolation Levels abnormal situation
Uncommitted Read Dirty reads, repeatable read, phantom read
Read Committed Repeatable read, phantom read
Non-repeatable read Magic Reading
Serialization

Guess you like

Origin www.cnblogs.com/littlepage/p/11791644.html