建表插入测试数据
A> create table test(id int ,num int) ;
Query OK, 0 rows affected (0.53 sec)

A> insert into test values(1,1);
Query OK, 1 row affected (0.01 sec)

A> insert into test values(2,2);
Query OK, 1 row affected (0.00 sec)

A> insert into test values(3,3);
Query OK, 1 row affected (0.01 sec)

A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)

A> show create table test ;
+-------+----------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------+
| test | CREATE TABLE `test` (
`id` int(11) DEFAULT NULL,
`num` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

一。隔离级别read uncommitted,事物A可以读到事物B的未提交数据,未提交数据称为脏数据,因此叫脏读。
A> set session transaction isolation level read uncommitted ;
Query OK, 0 rows affected (0.00 sec)

A> SELECT @@tx_isolation ;
+------------------+
| @@tx_isolation |
+------------------+
| READ-UNCOMMITTED |
+------------------+
1 row in set (0.00 sec)

A> start transaction ;
Query OK, 0 rows affected (0.00 sec)

A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)

B> start transaction ;
Query OK, 0 rows affected (0.00 sec)

B> update test set num=10 where id=1 ;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

B> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 10 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)

A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 10 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.01 sec)

B> rollback ;
Query OK, 0 rows affected (0.01 sec)

B> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)

A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)

二。隔离级别read committed ,
对于事物B未提交的DML操作,事物A是看不到的
但是事物A在本身事物操作的过程中(未做commit or rollback),可以读到事物B的已提交数据,这就是不可重复读
下面测试下:
A> set session transaction isolation level read committed ;
Query OK, 0 rows affected (0.00 sec)

A> start transaction ;
Query OK, 0 rows affected (0.00 sec)

A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)

B>start transaction ;
Query OK, 0 rows affected (0.00 sec)

B>update test set num=20 where id=1 ;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

B>rollback ;
Query OK, 0 rows affected (0.01 sec)

B>start transaction ;
Query OK, 0 rows affected (0.00 sec)

B>insert into test values(4,4) ;
Query OK, 1 row affected (0.00 sec)

B>select * from test;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
+------+------+
4 rows in set (0.00 sec)

B>rollback ;
Query OK, 0 rows affected (0.01 sec)

B>start transaction ;
Query OK, 0 rows affected (0.00 sec)

B>delete from test where id =1 ;
Query OK, 1 row affected (0.01 sec)

B>select * from test;
+------+------+
| id | num |
+------+------+
| 2 | 2 |
| 3 | 3 |
+------+------+
2 rows in set (0.00 sec)

B>rollback ;
Query OK, 0 rows affected (0.01 sec)

在事物B做这些DML操作的时候,事物A的查询结果一直没变,此时事物A尚未结束
A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)

提交事物B的某个操作,事物A里面的对表test的查询结果就变了,因此事物A里面对test表的查询是不可重复的,所以叫不可重复读

三。隔离级别repeatable read
mysql的默认事物隔离级别,防止了脏读和不可重复读,但可能出现幻读
该隔离级别解决了不重复读,保证了同一个事务里,查询的结果都是事务开始时的状态(一致性)。
但是,如果另一个事务同时提交了新数据,本事务再更新时,就会“惊奇的”发现了这些新数据,貌似之前读到的数据是“鬼影”一样的幻觉。

A> set session transaction isolation level repeatable read ;
Query OK, 0 rows affected (0.00 sec)

A> select @@session.tx_isolation ;
+------------------------+
| @@session.tx_isolation |
+------------------------+
| REPEATABLE-READ |
+------------------------+
1 row in set (0.00 sec)

A> start transaction ;
Query OK, 0 rows affected (0.00 sec)

A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)

B> start transaction ;
Query OK, 0 rows affected (0.00 sec)

B> update test set num=10 where id=1 ;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

B> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 10 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)

B> commit ;
Query OK, 0 rows affected (0.01 sec)

此处A的修改应该是num=3才对,但是因为幻读,事物B虽然提交了,但事物A读到的还是旧记录,
如同幻影一般,事物A修改后,发现结果是30,而不是想要的结果3
A> update test set num=num*3 where id=1 ;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0

A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 30 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)

A> commit ;
Query OK, 0 rows affected (0.01 sec)

四。隔离级别SERIALIZABLE
SERIALIZABLE事务隔离级别最严厉,在进行查询时就会对表或行加上共享锁,其他事务对该表将只能进行读操作,而不能进行写操作。

A> set session transaction isolation level serializable ;
Query OK, 0 rows affected (0.00 sec)

A> select @@session.tx_isolation ;
+------------------------+
| @@session.tx_isolation |
+------------------------+
| SERIALIZABLE |
+------------------------+
1 row in set (0.00 sec)

A> start transaction ;
Query OK, 0 rows affected (0.00 sec)

A> select * from test ;
+------+------+
| id | num |
+------+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+------+------+
3 rows in set (0.00 sec)

B> start transaction ;
Query OK, 0 rows affected (0.00 sec)

B> update test set num=3 where id=1 ;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

控制超时的参数
innodb_lock_wait_timeout | 120

深入理解mysql的隔离级别的更多相关文章

  1. 五分钟后,你将真正理解MySQL事务隔离级别!

    什么是事务? 事务是一组原子性的SQL操作,所有操作必须全部成功完成,如果其中有任何一个操作因为崩溃或其他原因无法执行,那么所有的操作都不会被执行.也就是说,事务内的操作,要么全部执行成功,要么全部执 ...

  2. MYSQL事件隔离级别以及复读,幻读,脏读的理解

    一.mysql事件隔离级别 1未提交读(READUNCOMMITTED) 另一个事务修改了数据,但尚未提交,而本事务中的SELECT会读到这些未被提交的数据(脏读)( 隔离级别最低,并发性能高 ) 2 ...

  3. [51CTO]新说MySQL事务隔离级别!

    新说MySQL事务隔离级别! 事务隔离级别这个问题,无论是校招还是社招,面试官都爱问!然而目前网上很多文章,说句实在话啊,我看了后我都怀疑作者弄懂没!本文所讲大部分内容,皆有官网作为佐证,因此对本文内 ...

  4. 查询mysql事务隔离级别

    查询mysql事务隔离级别 查询mysql事务隔离级别 分类: DB2011-11-26 13:12 2517人阅读 评论(0) 收藏 举报 mysqlsessionjava   1.查看当前会话隔离 ...

  5. mysql数据库隔离级别

    # 原创,转载请留言联系 事务的隔离级别 (由高到低)1.串行化(serializable):一个事务一个事务的执行2.可重复读(Repeatable-Read) 可重复读,无论其他事务是否修改并提交 ...

  6. Mysql事务-隔离级别

    MYSQL事务-隔离级别 事务是什么? 事务简言之就是一组SQL执行要么全部成功,要么全部失败.MYSQL的事务在存储引擎层实现. 事务都有ACID特性: 原子性(Atomicity):一个事务必须被 ...

  7. 查询和设置mysql事务隔离级别

    1.查看当前会话隔离级别 select @@tx_isolation; 2.查看系统当前隔离级别 select @@global.tx_isolation; 3.设置当前会话隔离级别 set sess ...

  8. mysql数据库隔离级别及其原理、Spring的7种事物传播行为

    一.事务的基本要素(ACID) 1.原子性(Atomicity):事务开始后所有操作,要么全部做完,要么全部不做,不可能停滞在中间环节.事务执行过程中出错,会回滚到事务开始前的状态,所有的操作就像没有 ...

  9. Mysql事务隔离级别和锁机制

    一.Spring支持四种事务隔离级别: 1.ISOLATION_READ_UNCOMMITTED(读未提交):这是事务最低的隔离级别,它充许令外一个事务可以看到这个事务未提交的数据. 2.ISOLAT ...

随机推荐

  1. wordpress重力表单实时提醒功能教程(亲测可用)

    小七在写项目的时候遇到了一个需求:用户在填写完成表单的各个字段后要提交到后台,但是后台程序狗不能一直守着后台吧,程序狗也需要陪女朋友啊,好做一个即时提醒的功能吧,再也不担心用户提交的内容被错过了,第一 ...

  2. Ubuntu sudo: add-apt-repository: command not found

    安装缺少的指令即可 $ sudo apt-get install software-properties-common python-software-properties

  3. erlang的格式化字符串

    往pgsql里面写数据的时候,不能双引号,开始纠结的不行,用拼字符串的形式,后来发现可以格式化字符串,泪奔 data_format.erl -module(data_format). -export( ...

  4. MUI 支付宝支付接入

    沙箱测试地址:https://openhome.alipay.com/platform/appDaily.htm 1资源下载地址:https://docs.open.alipay.com/54/106 ...

  5. Java虚拟机(一)之开篇

    写此类文章的初始动机:被同事问道 jvm 是做什么时,竟然茫然以对: 按照惯例,从 what/where/how 等开篇,即: 一. JVM 的目的是什么? 二. JVM 是什么时候被以何总形式被安装 ...

  6. Java字符代码中干掉制表符、回车符和换行符

    Java字符代码中干掉制表符.回车符和换行符 代码片段: String sql = StringUtils.trim(sql).replaceAll("[\\r\\n\\t]",& ...

  7. Centos如何设置IP地址,LINUX怎么修改IP地址

    对于很多刚刚接触linux的朋友来说,如何设置linux系统的IP地址,作为第一步,下面小编以centos系统为例,给大家演示如何给centos设置IP地址,如何修改linux 系统IP地址? 步骤阅 ...

  8. XML---Studying

    XML 被设计用来传输和存储数据. HTML 被设计用来显示数据. 1.什么是 XML? XML 指可扩展标记语言(EXtensible Markup Language) XML 是一种标记语言,很类 ...

  9. centos 6.3 kickstart 装机卡在 unsupported hardware detected 页面

    起因 最近厂里一批 dell 新机器 centos 6.3 装机卡在 Unsupported Hardware Detected 页面,要人肉点击 OK 才能继续装机: Unsupported Har ...

  10. leetcode278

    /* The isBadVersion API is defined in the parent class VersionControl. bool IsBadVersion(int version ...