Mysql 的事务隔离

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xxzhaobb/article/details/84024164

测试环境:mysql版本

mysql> select @@version;
+-----------+
| @@version |
+-----------+
| 5.6.42    |
+-----------+
1 row in set (0.00 sec)

mysql>

Mysql的事务隔离

标题


-- read-uncommitted ,读未提交

--session 1 

mysql> show variables like '%iso%';
+---------------+------------------+
| Variable_name | Value            |
+---------------+------------------+
| tx_isolation  | READ-UNCOMMITTED |
+---------------+------------------+
1 row in set (0.00 sec)

mysql> select * from t1;
+------+-------+------+
| year | month | day  |
+------+-------+------+
| 2000 |    01 |   01 |
| 2000 |    01 |   20 |
| 2000 |    01 |   30 |
| 2000 |    02 |   02 |
| 2000 |    02 |   23 |
| 2000 |    02 |   23 |
| 2018 |    10 |   28 |
| 2018 |    11 |   05 |
| 2018 |    11 |   04 |
| 2018 |    11 |   03 |
| 2018 |    11 |   06 |
| 2018 |    11 |   07 |
+------+-------+------+
12 rows in set (0.00 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t1 values(2018,11,08);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+------+-------+------+
| year | month | day  |
+------+-------+------+
| 2000 |    01 |   01 |
| 2000 |    01 |   20 |
| 2000 |    01 |   30 |
| 2000 |    02 |   02 |
| 2000 |    02 |   23 |
| 2000 |    02 |   23 |
| 2018 |    10 |   28 |
| 2018 |    11 |   05 |
| 2018 |    11 |   04 |
| 2018 |    11 |   03 |
| 2018 |    11 |   06 |
| 2018 |    11 |   07 |
| 2018 |    11 |   08 |
+------+-------+------+
13 rows in set (0.00 sec)

mysql>

--session 2

mysql> select * from t1;
+------+-------+------+
| year | month | day  |
+------+-------+------+
| 2000 |    01 |   01 |
| 2000 |    01 |   20 |
| 2000 |    01 |   30 |
| 2000 |    02 |   02 |
| 2000 |    02 |   23 |
| 2000 |    02 |   23 |
| 2018 |    10 |   28 |
| 2018 |    11 |   05 |
| 2018 |    11 |   04 |
| 2018 |    11 |   03 |
| 2018 |    11 |   06 |
| 2018 |    11 |   07 |
| 2018 |    11 |   08 |
+------+-------+------+
13 rows in set (0.00 sec)

mysql>


从上面的测试,可以看到,一个事务,并没有commit提交,在其他的会话中,可以看到这个事务。也就是一个事务可以读取其他事务没有提交的数据 。

-- read committed  。类似Oracle的情况

-- session 1 ,插入数据,未提交

mysql> show variables like '%iso%';
+---------------+----------------+
| Variable_name | Value          |
+---------------+----------------+
| tx_isolation  | READ-COMMITTED |
+---------------+----------------+
1 row in set (0.00 sec)

mysql> select * from t1;
+------+-------+------+
| year | month | day  |
+------+-------+------+
| 2000 |    01 |   01 |
| 2000 |    01 |   20 |
| 2000 |    01 |   30 |
| 2000 |    02 |   02 |
| 2000 |    02 |   23 |
| 2000 |    02 |   23 |
| 2018 |    10 |   28 |
| 2018 |    11 |   05 |
| 2018 |    11 |   04 |
| 2018 |    11 |   03 |
| 2018 |    11 |   06 |
| 2018 |    11 |   07 |
+------+-------+------+
12 rows in set (0.00 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t1 values (2018,11,08);
Query OK, 1 row affected (0.00 sec)

mysql>

-- session 2上查看  ,没有读取到新插入的数据

mysql> select * from t1;
+------+-------+------+
| year | month | day  |
+------+-------+------+
| 2000 |    01 |   01 |
| 2000 |    01 |   20 |
| 2000 |    01 |   30 |
| 2000 |    02 |   02 |
| 2000 |    02 |   23 |
| 2000 |    02 |   23 |
| 2018 |    10 |   28 |
| 2018 |    11 |   05 |
| 2018 |    11 |   04 |
| 2018 |    11 |   03 |
| 2018 |    11 |   06 |
| 2018 |    11 |   07 |
+------+-------+------+
12 rows in set (0.00 sec)

mysql>

-- session 1 提交 ,session 2 再次查看,发现看到了提交后的数据

mysql> commit;
Query OK, 0 rows affected (0.02 sec)

mysql>

mysql> select * from t1;
+------+-------+------+
| year | month | day  |
+------+-------+------+
| 2000 |    01 |   01 |
| 2000 |    01 |   20 |
| 2000 |    01 |   30 |
| 2000 |    02 |   02 |
| 2000 |    02 |   23 |
| 2000 |    02 |   23 |
| 2018 |    10 |   28 |
| 2018 |    11 |   05 |
| 2018 |    11 |   04 |
| 2018 |    11 |   03 |
| 2018 |    11 |   06 |
| 2018 |    11 |   07 |
| 2018 |    11 |   08 |
+------+-------+------+
13 rows in set (0.00 sec)

mysql>

-- 可重复读 ,repeatable-read  
-- 是mysql数据库的默认的事务隔离级别。消除了脏读,不可重复读,幻读,很好地保证了事务的一致性。


-- session 1 ,开启一个事务,插入数据,并提交数据。

mysql> show variables like '%iso%';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| tx_isolation  | REPEATABLE-READ |
+---------------+-----------------+
1 row in set (0.01 sec)


mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t1 values (2018,11,09);
Query OK, 1 row affected (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.11 sec)

mysql>

mysql> select * from t1;
+------+-------+------+
| year | month | day  |
+------+-------+------+
| 2000 |    01 |   01 |
| 2000 |    01 |   20 |
| 2000 |    01 |   30 |
| 2000 |    02 |   02 |
| 2000 |    02 |   23 |
| 2000 |    02 |   23 |
| 2018 |    10 |   28 |
| 2018 |    11 |   05 |
| 2018 |    11 |   04 |
| 2018 |    11 |   03 |
| 2018 |    11 |   06 |
| 2018 |    11 |   07 |
| 2018 |    11 |   08 |
| 2018 |    11 |   09 |
+------+-------+------+
14 rows in set (0.00 sec)

mysql> insert into t1 values (2018,11,10);
Query OK, 1 row affected (0.02 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from t1;
+------+-------+------+
| year | month | day  |
+------+-------+------+
| 2000 |    01 |   01 |
| 2000 |    01 |   20 |
| 2000 |    01 |   30 |
| 2000 |    02 |   02 |
| 2000 |    02 |   23 |
| 2000 |    02 |   23 |
| 2018 |    10 |   28 |
| 2018 |    11 |   05 |
| 2018 |    11 |   04 |
| 2018 |    11 |   03 |
| 2018 |    11 |   06 |
| 2018 |    11 |   07 |
| 2018 |    11 |   08 |
| 2018 |    11 |   09 |
| 2018 |    11 |   10 |
+------+-------+------+
15 rows in set (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql>

-- session 2 ,在session 2 的事务中,看不到session1 中提交的数据。如果需要查看到,则在查询语句中增加for update ;

mysql> begin;
Query OK, 0 rows affected (0.00 sec)


mysql>
mysql> select * from t1;
+------+-------+------+
| year | month | day  |
+------+-------+------+
| 2000 |    01 |   01 |
| 2000 |    01 |   20 |
| 2000 |    01 |   30 |
| 2000 |    02 |   02 |
| 2000 |    02 |   23 |
| 2000 |    02 |   23 |
| 2018 |    10 |   28 |
| 2018 |    11 |   05 |
| 2018 |    11 |   04 |
| 2018 |    11 |   03 |
| 2018 |    11 |   06 |
| 2018 |    11 |   07 |
| 2018 |    11 |   08 |
| 2018 |    11 |   09 |
+------+-------+------+
14 rows in set (0.00 sec)

-- 增加for update ,可以看到新增加的数据了 。

mysql> mysql> select * from t1 for update;
+------+-------+------+
| year | month | day  |
+------+-------+------+
| 2000 |    01 |   01 |
| 2000 |    01 |   20 |
| 2000 |    01 |   30 |
| 2000 |    02 |   02 |
| 2000 |    02 |   23 |
| 2000 |    02 |   23 |
| 2018 |    10 |   28 |
| 2018 |    11 |   05 |
| 2018 |    11 |   04 |
| 2018 |    11 |   03 |
| 2018 |    11 |   06 |
| 2018 |    11 |   07 |
| 2018 |    11 |   08 |
| 2018 |    11 |   09 |
| 2018 |    11 |   10 |
+------+-------+------+
15 rows in set (0.00 sec)

mysql>

END

猜你喜欢

转载自blog.csdn.net/xxzhaobb/article/details/84024164