MySQL的隔离性测试

Posted adream307

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL的隔离性测试相关的知识,希望对你有一定的参考价值。

在阅读本文前,请思考以下分别输出什么

操作序列1

  1. 在数据库中存入 A=1
  2. Client1启动一个事物T1
  3. Client2启动一个事物T2
  4. Client1 读取 A 的值 X1
  5. Client2设置 A=2
  6. Client2提交事物T2
  7. Client1 读取 A 的值 X2
  8. Client1提交事物T1
  9. Client1 读取 A 的值 X3
    请问 X1 X2 X3 分别为什么值?

操作序列2

  1. 在数据库中存入 A=1
  2. Client1启动一个事物T1
  3. Client2启动一个事物T2
  4. Client2设置 A=2
  5. Client2提交事物T2
  6. Client1 读取 A 的值 X1
  7. Client1提交事物T1
  8. Client1 读取 A 的值 X2
    请问 X1 X2 分别为什么值?

启动 docker

使用docker 启动 mysql, 所有设置均采用默认配置

docker pull mysql:8
docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306  -d mysql:8

原始数据

mysql> create table t1(a int);
Query OK, 0 rows affected (0.05 sec)

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

mysql> select * from t1;
+------+
| a    |
+------+
|    1 |
+------+

链接 Client1Client2

mysql --user=root --password=123456  --port=3306 --host=127.0.0.1 --database=test

操作序列1

Client1 启动事物

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

Client2 启动事物

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

Client1 读取a的值

mysql> select * from t1;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

Client2 设置a=2 并提交事物

mysql> update t1 set a=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from t1;
+------+
| a    |
+------+
|    2 |
+------+
1 row in set (0.00 sec)

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

mysql> select * from t1;
+------+
| a    |
+------+
|    2 |
+------+
1 row in set (0.00 sec)

Client1 读取a 的值,并提交事物,然后再读取a的值

mysql> select * from t1;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

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

mysql> select * from t1;
+------+
| a    |
+------+
|    2 |
+------+
1 row in set (0.00 sec)

因此在 mysqlX1=1, X2=1, X3=2

操作序列2

删除表格并重新插入数据

mysql> drop table t1;
Query OK, 0 rows affected (0.04 sec)

mysql> create table t1(a int);
Query OK, 0 rows affected (0.05 sec)

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

mysql> select * from t1;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

Client1 启动事物

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

Client2 启动事物

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

Client2 设置a=2 并提交事物

mysql> update t1 set a=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from t1;
+------+
| a    |
+------+
|    2 |
+------+
1 row in set (0.00 sec)

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

mysql> select * from t1;
+------+
| a    |
+------+
|    2 |
+------+
1 row in set (0.01 sec)

Client1 读取a 的值,并提交事物,然后再读取a的值

mysql> select * from t1;
+------+
| a    |
+------+
|    2 |
+------+
1 row in set (0.00 sec)

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

mysql> select * from t1;
+------+
| a    |
+------+
|    2 |
+------+
1 row in set (0.00 sec)

因此在 mysqlX1=2, X2=2

以上是关于MySQL的隔离性测试的主要内容,如果未能解决你的问题,请参考以下文章

MySQL的四种事务隔离级别

MySQL的四种事务隔离级别

MySQL事务与存储引擎

MySQL中事务隔离级别

MySQL的四种事务隔离级别

MySQL的四种事务隔离级别