You understand a text with dirty reads, phantom reads, non-repeatable read and mysql locks, transaction isolation mechanism

First talk about the four characteristics of a database transaction

1 ACID

    事务的四大特性是ACID(不是"酸"....)

(1) A: Atomicity (Atomicity)

Atomicity means that a transaction is either all executed or not executed at all.

(2) C: Consistency (Consistency)

When the transaction is completed, the data must be in a consistent state. If the error transactions executed on the way, before the transaction is not rolled back to the state before the execution, so the data is in a consistent state. If the transaction is not rolled back after the error, modify the content portion written to the database, then the data is inconsistent state.

(3) I: isolation (Isolation)

Simultaneously processing a plurality of transactions, a transaction can not be performed to another firm interference, the internal transaction operations other concurrent transaction isolation.

(4) D: Persistent (Durability Rev)

After the transaction commits, changes to the data is permanent.


2 Mysql lock

Mysql lock can actually take many forms by Category:

  • Press the locking mechanism can be divided into optimistic and pessimistic locking lock.
  • According to the compatibility can be divided into S and X locks the lock.
  • Press lock granularity can be divided into table locks, row locks, page locks.
  • Press lock mode can be divided into record locking, gap lock, next-key locks, lock intent, intent lock insert.

S here focuses on locks, X locks, optimistic and pessimistic locking lock.

(1) S and X locks lock

S lock to X lock is two standard engine InnoDB row lock mechanism implemented. The default view engine can be used

show variables like '%storage_engine%';

Mysql version 8.0.17 of the results are as follows:
Here Insert Picture Description
first built test libraries and test table, very simple table on two fields.

create database test;
use test;
create table a
(
id int primary key auto_increment,
money int
);

Here Insert Picture Description

Ⅰ.S lock

S lock is also called shared locks, read locks, data can only be read can not be modified.

lock table a read;

Then .....
Here Insert Picture Description
only read can not be changed, deleted, it can not be increased.

Ⅱ.X lock

X lock is also called exclusive lock, write lock, lock the table after a transaction, other transactions can not be locked with the additions and deletions to change search operation.

Set manual submission, open transactions, the X-lock.

set autocmmmit=0;
start transaction;
lock table a write;

Here Insert Picture Description
In open another transaction, use the select statement.

set autocommit=0;
start transaction;
select * from a;

Here Insert Picture Description
Here is blocking select operation, because there has been no release X lock.
Here Insert Picture Description
The same can no longer be locked, but also blocking.
Here Insert Picture Description
Going back to the lock transaction, ah, what did not, read and write properly.
Here Insert Picture Description
After the release of the lock:

unlock table;

Here Insert Picture Description
Here Insert Picture Description
In another transaction can be seen in downtime.


(2) 乐观锁与悲观锁

Ⅰ.乐观锁

乐观锁就是总是假设是最好的情况,每次去操作的时候都不会上锁,但在更新时会判断有没有其他操作去更新这个数据,是一种宽松的加锁机制.
mysql本身没有提供乐观锁的支持,需要自己来实现,常用的方法有版本控制和时间戳控制两种.

  • 版本控制
    版本控制就是为表增加一个version字段,读取数据时连同这个version字段一起读出来,之后进行更新操作,版本号加1,再将提交的数据的版本号与数据库中的版本号进行比较,若提交的数据的版本号大于数据库中的版本号才会进行更新.

    举个例子,假设此时version=1,A进行操作,更新数据后version=2,与此同时B也进行操作,更新数据后version=2,A先完成操作,率先将数据库中的version设置为2,此时B提交,B的version与数据库中的version一样,不接受B的提交.

  • 时间戳控制
    时间戳控制与版本控制差不多,把version字段改为timestamp字段

还有一种实现方法叫CAS算法,这个作者不怎么了解,有兴趣可以自行搜索.

Ⅱ.悲观锁

悲观锁就是总是假设最坏的情况,在整个数据处理状态中数据处于锁定状态,悲观锁的实现往往依靠数据库的锁机制.每次在拿到数据前都会上锁.
mysql在调用一些语句时会上悲观锁,如(先关闭自动提交,开启事务):

set autocommit=0;
start transaction;

Here Insert Picture Description
两个事务都这样操作,然后其中一个事务输入:

select * from a where xxx for update;

Here Insert Picture Description
在另一事务也这样输入:
Here Insert Picture Description
这时语句会被阻塞,直到上锁的那个事务commit(解开悲观锁).
Here Insert Picture Description
Here Insert Picture Description
在另一事务中可以看到这个事务被阻塞了2.81s.

*** lock in share mode.

也会加上悲观锁.


4 脏读,幻读,不可重复读与两类丢失更新

(1) 脏读

脏读是指一个事务读取到了另一事务未提交的数据,造成select前后数据不一致.

比如事务A修改了一些数据,但没有提交,此时事务B却读取了,这时事务B就形成了脏读,一般事务A的后续操作是回滚,事务B读取到了临时数值.

事务A 事务B
开始事务 开始事务
更新X,旧值X=1,新值X=2  
   读取X,X=2(脏读)
回滚X=1  
结束事务(X=1) 结束事务

(2) 幻读

幻读是指并不是指同一个事务执行两次相同的select语句得到的结果不同,
而是指select时不存在某记录,但准备插入时发现此记录已存在,无法插入,这就产生了幻读.

事务A 事务B
开始事务 开始事务
select某个数据为空,准备插入一个新数据  
   插入一个新数据
   提交,结束事务
插入数据,发现插入失败,由于事务B已插入相同数据   
结束事务  

(3) 不可重复读

不可重复读指一个事务读取到了另一事务已提交的数据,造成select前后数据不一致.
比如事务A修改了一些数据并且提交了,此时事务B却读取了,这时事务B就形成了不可重复读.

事务A 事务B
开始事务 开始事务
读取X=1 读取X=1
更新X=2  
提交,结束事务  
   读取X=2
   结束事务

(4) 第一类丢失更新

第一类丢失更新就是两个事务同时更新一个数据,一个事务更新完毕并提交后,另一个事务回滚,造成提交的更新丢失.

事务A 事务B
开始事务 开始事务
读取X=1 读取X=1
修改X=2 修改X=3
   提交,结束事务
回滚  
结束事务(X=1) X=1,X本应为提交的3

(5) 第二类丢失更新

第二类丢失更新就是两个事务同时更新一个数据,先更新的事务提交的数据会被后更新的事务提交的数据覆盖,即先更新的事务提交的数据丢失.

事务A 事务B
开始事务 开始事务
读取X=1 读取X=1
更新X=2  
提交事务,X=2,结束  
   更新X=3
   提交事务,X=3,事务A的更新丢失,结束

5 封锁协议与隔离级别

封锁协议就是在用X锁或S锁时制定的一些规则,比如锁的持续时间,锁的加锁时间等.不同的封锁协议对应不同的隔离级别.事务的隔离级别一共有4种,由低到高分别是Read uncommitted,Read committed,Repeatable read,Serializable,分别对应的相应的封锁协议等级.

(1) 一级封锁协议

一级封锁协议对应的是Read uncommitted隔离级别,Read uncommitted,读未提交,一个事务可以读取另一个事务未提交的数据,这是最低的级别.一级封锁协议本质上是在事务修改数据之前加上X锁,直到事务结束后才释放,事务结束包括正常结束(commit)与非正常结束(rollback).

一级封锁协议不会造成更新丢失,但可能引发脏读,幻读,不可重复读.
设置手动提交与事务隔离等级为read uncommited,并开启事务(注意要先设置事务等级再开启事务).

set autocommit=0;
set session transaction isolation level read uncommitted;
start transaction;

Here Insert Picture Description
(中间有一行打多了一个t可以忽略.....)

a.引发脏读

在一个事务中修改表中的值,不提交,另一个事务可以select到未提交的值.
Here Insert Picture Description
Here Insert Picture Description
出现了脏读.

b.引发幻读

在一个事务中插入一条数据,提交.
Here Insert Picture Description
另一事务中select时没有,准备insert,但是insert时却提示已经存在.引发幻读.
Here Insert Picture Description

c.引发不可重复读

未操作提交前:
Here Insert Picture Description
另一事务修改并提交:
Here Insert Picture Description
再次读:
Here Insert Picture Description
引发不可重复读.

(2) 二级封锁协议

二级封锁协议本质上在一级协议的基础上(在修改数据时加X锁),在读数据时加上S锁,读完后立即释放S锁,可以避免脏读.但有可能出现不可重复读与幻读.二级封锁协议对应的是Read committed与Repeatable Read隔离级别.
先设置隔离等级

set session transaction isolation level read committed;

Ⅰ.Read committed

Read committed,读提交,读提交可以避免脏读,但可能出现幻读与不可重复读.

a.避免脏读

开启一个事务并更新值,在这个事务中money=100(更新后)
Here Insert Picture Description
另一事务中money为未更新前的值,这就避免了脏读.
Here Insert Picture Description
注意,事实上脏读在read committed隔离级别下是不被允许的,但是mysql不会阻塞查询,而是返回未修改之前数据的备份,这种机制叫MVCC机制(多版本并发控制).

b.引发幻读

在一个事务中插入数据并提交.
Here Insert Picture Description
另一事务中不能插入"不存在"的数据,出现幻读.
Here Insert Picture Description

c.引发不可重复读

事务修改并提交前:
Here Insert Picture Description
事务修改并提交:
Here Insert Picture Description
出现不可重复读.
Here Insert Picture Description

Ⅱ.Repeatable read

Repeatable read比Read committed严格一点,是Mysql的默认级别,读取过程更多地受到MVCC影响,可防止不可重复读与脏读,但仍有可能出现幻读.

a.避免脏读

在一个事务中修改数据,不提交.
Here Insert Picture Description
另一事务中两次select的结果都不变,没有出现脏读.
Here Insert Picture Description

b.避免不可重复读

一个事务修改数据并提交.
Here Insert Picture Description
另一事务中select的结果没有发生改变,即没有出现不可重复读.
Here Insert Picture Description

c.引发幻读

同理,一个事务插入一条数据并提交.
Here Insert Picture Description
另一个事务插入时出现幻读.
Here Insert Picture Description

(3) 三级封锁协议

三级封锁协议,在一级封锁协议的基础上(修改时加X锁),读数据时加上S锁(与二级类似),但是直到事务结束后才释放S锁,可以避免幻读,脏读与不可重复读.三级封锁协议对应的隔离级别是Serializable.
先设置Serializable隔离级别

set session transaction isolation level serializable

a.避免脏读

设置事务隔离等级后开启事务并update,发现堵塞.从而避免了脏读.
Here Insert Picture Description

b.避免幻读

插入时直接阻塞,避免了幻读.
Here Insert Picture Description

c.避免不可重复读

In the case of a dirty read can know, update would be blocked, they can not commit the transaction, thus avoiding the non-repeatable read.


6 two-phase locking protocol

Services must be divided into two phases of data locking and unlocking, both ends of the locking protocol called 2PL (not 2PC), all are locked in before unlocking.

(1) Lock

Lock will update or

select *** for update
*** lock in share mode

When

(2) unlocked

Unlock carried out at the end of the transaction, including the end of the transaction rollback and commit.


7. Finally

It is the author's address CSDN
Finally, here are the author's public number, to learn together, grow together.
Here Insert Picture Description

Reference links
. 1: Acid1
2: ACID2
. 3: MySQL lock 1 of
4: optimistic and pessimistic locking lock 1
5: optimistic and pessimistic locking lock 2
6: optimistic and pessimistic locking lock. 3
. 7: MySQL modify transaction isolation level
. 8: MySQL three Sec blockade and lock
9: the database locking protocol
10: MySQL transaction isolation mechanism. 1
. 11: MySQL transaction isolation mechanism 2
12 is: MySQL phantom read
13 is: MySQL dirty reads, non-repeatable read and phantom read
14: MySQL two lock 1
15: mysql two locks 2

Guess you like

Origin blog.51cto.com/14415843/2443420