mysql transaction isolation level experiments

First, the experimental data:

Construction of the table statement:

CREATE TABLE `isolation` (
`id` int(11) NOT NULL,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_croatian_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

 

Create a table named: isolation table, which has two fields, a id, a name

Raw data:

insert into ISOLATION values(1,'name1'),(2,'name2'),(3,'name3');

Insert three records.

Second, prepare:

Respectively two open mysql client A, B

View current client isolation level

SELECT @@tx_isolation

 mysql InnoDB default isolation level is repeatable read.

 

Set B clients do not automatically submit:

View auto-commit setting:

show session variables like 'autocommit';

mysql InnoDB the default auto-commit.

Settings are not automatically submit:

set session autocommit=0;

Client settings are not automatically submit things to succeed. 

First, the start of the experiment dirty reads ---------------------------------------

A set of isolation level to read uncommitted:

set session transaction isolation level read uncommitted;

Client B to start a thing

We found no update data submitted to the school, resulting in dirty read after the rollback.

 

Discovery insert data is not submitted to the school, resulting in dirty read after the rollback.

 

It found that delete data not submitted to the school, resulting in dirty read after the rollback.

脏读实验结束---------------------------------------

结论,脏读会读没有提交的update insert delete。

 

读已提交实验开始---------------------------------------

设置A的隔离级别为读已提交:

set session transaction isolation level read committed;

update的时候------>>>发现解决了脏读,但是在一个事物里面,两次查询的结果不一致,即解决不了重复读的问题

insert的时候------>>>发现解决了脏读,但是在一个事物里面,两次查询的结果不一致,即解决不了重复读的问题

delete的时候------>>>发现解决了脏读,但是在一个事物里面,两次查询的结果不一致,即解决不了重复读的问题

 读已提交实验结束---------------------------------------

结论:解决了脏读,但是一个事物里面,同一个查询结果不一致,不能解决重复读的问题。

 

可重复读实验开始---------------------------------------

数据重新清理一下:

update的时候------>>>在一个事物里面,两次查询直间,读不到另外事物提交的内容,只有提交当前事物,新启事物查询的时候才能查到更新的,即解决不了重复读的问题

 

delete的时候------>>>在一个事物里面,两次查询直间,读不到另外事物提交的内容,只有提交当前事物,新启事物查询的时候才能查到更新的,即解决不了重复读的问题

 

 insert的时候------>>>在一个事物里面,两次查询直间,读不到另外事物提交的内容,只有提交当前事物,新启事物查询的时候才能查到更新的,即解决不了重复读的问题

可重复读实验结束---------------------------------------

结论:解决了脏读,可重复读,好像mysql 5.7.26在可重复读隔离级别解决了 幻想读的现象(猜测,还没太明白)。

 

串行化读实验开始---------------------------------------

串行化读实验结束--------------------------------------

结论:串行化后,当一个事物没有提交,另外的事物不能修改对数据进行(update,insert,delete操作)。

Guess you like

Origin www.cnblogs.com/likunxuhui/p/10985588.html