mysql事务的隔离级别

事务的隔离级别
  1. read uncommitted:产生脏读、不可重复读、幻读

    在session-1内开启事务进行插入操作,而不提交

mysql> set session transaction isolation level read uncommitted;
Query OK, 0 rows affected
mysql> select @@tx_isolation;
+------------------+
| @@tx_isolation   |
+------------------+
| READ-UNCOMMITTED |
+------------------+
1 row in set

mysql> start transaction;
Query OK, 0 rows affected

mysql> insert into a_student values(0,'Li',20,'2018-7-25');
Query OK, 1 row affected

mysql> commit;
Query OK, 0 rows affected

​ 在一个session-2内开启事务进行查询,查到了session-1内未提交的数据;产生了脏读

mysql> set session transaction isolation level read uncommitted;
Query OK, 0 rows affected

mysql> start transaction;
Query OK, 0 rows affected

mysql> select * from a_student where id = 0;
+----+-------+------+---------------------+
| id | sname | sage | stime               |
+----+-------+------+---------------------+
|  0 | Li    |   20 | 2018-07-25 00:00:00 |
+----+-------+------+---------------------+
1 row in set

mysql> commit;
Query OK, 0 rows affected

mysql>    

2.read committed:产生不可重复读,幻读

​ 在session-1中查询一条记录,在session-2修改这条记录,在session-1中查询同一条记录结果发生了变化。

mysql> set session transaction isolation level read committed;
Query OK, 0 rows affected

mysql> select @@tx_isolation;
+----------------+
| @@tx_isolation |
+----------------+
| READ-COMMITTED |
+----------------+
1 row in set

mysql> start transaction;
Query OK, 0 rows affected

mysql> select * from a_student where id = 0;
+----+-------+------+---------------------+
| id | sname | sage | stime               |
+----+-------+------+---------------------+
|  0 | Li    |   20 | 2018-07-25 00:00:00 |
+----+-------+------+---------------------+
1 row in set
mysql> select * from a_student where id = 0;
+----+-------+------+---------------------+
| id | sname | sage | stime               |
+----+-------+------+---------------------+
|  0 | Liu   |   20 | 2018-07-25 00:00:00 |
+----+-------+------+---------------------+
1 row in set

mysql> commit;
Query OK, 0 rows affected

​ session-2修改记录。

mysql> set session transaction isolation level read committed;
Query OK, 0 rows affected

mysql> select @@tx_isolation;
+----------------+
| @@tx_isolation |
+----------------+
| READ-COMMITTED |
+----------------+
1 row in set

mysql> start transaction;
Query OK, 0 rows affected

mysql> update a_student set sname='Liu' where id=0;
Query OK, 1 row affected
Rows matched: 1  Changed: 1  Warnings: 0
mysql> commit;
Query OK, 0 rows affected

3.repeatable read:有可能产生幻读

4.串行化、序列化

脏读不可重复读幻读
insertupdateinsert
隔离等级脏读不可重复读幻读
读未提交NONONO
读已提交YESNONO
可重复读YESYESYES/NO
串行化YESYESYES
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值