1.什么是事务
    

      事务执行是一个整体,所有的SQL语句都必须执行成功。如果其中有1条SQL语句出现异常,则所有的SQL语句都要回滚,整个业务执行失败。

mysql 在开始某个事务的时候,会隐式提交上一个事务。所以 MySQL 本身是不支持事务嵌套的。

但 MySQL 也给我们提供了一个 SAVEPOINT 来做出类似事务嵌套的动作,我们将运用 SAVEPOINT 来帮助我们实现事务嵌套。

mysql 事务里面能套事务吗 mysql 事务嵌套_mysql 事务里面能套事务吗

 

1)原子性:事务是一个不可分割的工作单位,要么同时成功,要么同时失败。例:当两个人发起转账业务时,如果A转账发起,而B因为一些原因不能成功接受,事务最终将不会提交,则A和B的请求最终不会成功。

2)持久性:一旦事务提交,他对数据库的改变就是永久的。注:只要提交了事务,将会对数据库的数据进行永久性刷新。

3)隔离性:多个事务之间相互隔离的,互不干扰

4)一致性:事务执行接收之后,数据库完整性不被破坏

注意:只有当前三条性质都满足了,才能保证事务的一致性

mysql 事务里面能套事务吗 mysql 事务嵌套_mysql 事务里面能套事务吗_02

刷脏:Mysql为了保证存储效率,于是每次将要读写的文件是先存储在缓存池中,对于数据的操作是在缓存池中,而mysql将会定期的刷新到磁盘中。

1、如何保证原子性:

首先:对于A和B两操作要操作成功就一定需要更改到表的信息,如果如图所示A语句操作成功,而B语句操作时出现断电等其他情况终止了操作,所以此时两个事务没有操作成功,在没有提交事务之前,mysql会先记录跟新前的数据到undo log日志里面,当最终的因为操作不成功而发生事务回滚时,会从undo log日志里面先前存好的数据,重新对数据库的数据进行数据的回退。

undo log日志:(撤销回退的日志)主要存储数据库更新之前的数据,用于作备份

2、如何保证事务的持久性:

通过重做日志:redo log日志,对于用户将对发生了修改而为提交的数据存入了redo log日志中,当此时发生断电等其他异常时,可以根据redo log日志重新对数据做一个提交,做一个恢复。

mysql 事务里面能套事务吗 mysql 事务嵌套_mysql_03

mysql 事务里面能套事务吗 mysql 事务嵌套_mysql_04

 

 

 

 

2.手动提交事务

MYSQL中可以有两种方式进行事务的操作:
1)手动提交事务
2)自动提交事务

2.1手动提交事务的SQL语句

功能

SQL语句

开启事务

start transaction;

提交事务

commit;

回滚事务

rollback;

2.2手动提交事务使用过程:

  1. 执行成功的情况: 开启事务–>执行多条SQL语句 --> 成功提交事务
  2. 执行失败的情况: 开启事务 --> 执行多条SQL语句 --> 事务的回滚

总结: 如果事务中SQL语句没有问题,commit提交事务,会对数据库数据的数据进行改变。 如果事务中SQL语句有问题,rollback回滚事务,会回退到开启事务时的状态。

3.自动提交事务

MySQL默认每一条DML(增删改)语句都是一个单独的事务,每条语句都会自动开启一个事务,语句执行完毕自动提交事务,MySQL默认开始自动提交事务.

3.1案例自动提交事务

 一个shell:

mysql> update user1 set address="tianjin" where id=14;
Query OK, 0 rows affected (0.01 sec)
Rows matched: 1  Changed: 0  Warnings: 0

另一个窗口shell:

mysql> select * from user1 where id=14;
+----+--------+------+---------+
| id | name   | age  | address |
+----+--------+------+---------+
| 14 | wubo11 |  111 | tianjin |
+----+--------+------+---------+
1 row in set (0.00 sec)

3.2取消自动提交

@@表示全局变量,1表示开启,0表示关闭  mysql默认的隔离级别是提交之后可读

A:shell:

mysql> select @@tx_isolation;
+----------------+
| @@tx_isolation |
+----------------+
| READ-COMMITTED |
+----------------+
1 row in set, 1 warning (0.00 sec)
mysql> select @@autocommit;
+--------------+
| @@autocommit |
+--------------+
|            1 |
+--------------+
1 row in set (0.00 sec)

mysql> set @@autocommit=0;
Query OK, 0 rows affected (0.00 sec)

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

mysql> update user1 set address="wubo" where id=14;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from user1 where id=14;
+----+--------+------+---------+
| id | name   | age  | address |
+----+--------+------+---------+
| 14 | wubo11 |  111 | wubo    |
+----+--------+------+---------+
1 row in set (0.00 sec)

B:shell:由于A shell没有关闭自动autocommit,而且也没有手动commit,所以这里看不到更新的数据

mysql> select * from user1 where id=14;
+----+--------+------+---------+
| id | name   | age  | address |
+----+--------+------+---------+
| 14 | wubo11 |  111 | tianjin |
+----+--------+------+---------+
1 row in set (0.00 sec)

A:shell 手动提交事务commmit

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

B shell:此时就可以看到数据了。

mysql> select * from user1 where id=14;
+----+--------+------+---------+
| id | name   | age  | address |
+----+--------+------+---------+
| 14 | wubo11 |  111 | wubo    |
+----+--------+------+---------+
1 row in set (0.00 sec)

4.事务原理

事务开启之后, 所有的操作都会临时保存到事务日志中, 事务日志只有在得到commit命令才会同步到数据表中,其他任何情况都会清空事务日志(rollback,断开连接)

mysql 事务里面能套事务吗 mysql 事务嵌套_mysql 事务里面能套事务吗_05

 

4.2 事务的步骤:

  1. 客户端连接数据库服务器,创建连接时创建此用户临时日志文件
  2. 开启事务以后,所有的操作都会先写入到临时日志文件
  3. 所有的查询操作从表中查询,但会经过日志文件加工后才返回
  4. 如果事务提交则将日志文件中的数据写到表中否则清空日志文件

5.回滚点

5.1 什么是回滚点

在某些成功的操作完成之后,后续的操作有可能成功有可能失败,但是不管成功还是失败,前面操作都已经成功,可以在当前成功的位置设置一个回滚点。可以供后续失败操作返回到该位置,而不是返回所有操作,这个点称之为回滚点。

5.2 回滚点的操作语句

回滚点的操作语句

语句

设置回滚点

savepoint 名字

回到回滚点

rollback to 名字

5.3 具体操作:

  1. 查询目前数据
  2. 开启事务
  3. 操作书库
  4. 设置回滚点:savepoint name;
  5. 在操作数据库
  6. 回到回滚点:rollback to name
  7. 分析执行过程

总结:设置回滚点可以让我们在失败的时候回到回滚点,而不是回到事务开启的时候。

6.事务的隔离级别

6.1事务的四大特性ACID

事务特性

含义

原子性(Atomicity)

每个事务都是一个整体,不可再拆分,事务中所有的SQL语句要么都执行成功,要么都失败。

一致性(Consistency)

事务在执行前数据库的状态与执行后数据库的状态保持一致。如:转账前2个人的总金额是2000,转账后2个人总金额也是2000

隔离性(Isolation)

事务与事务之间不应该相互影响,执行时保持隔离的状态。

持久性(Durability)

一旦事务执行成功,对数据库的修改是持久的。就算关机,也是保存下来的。

6.2事务的隔离级别

事务在操作时的理想状态: 所有的事务之间保持隔离,互不影响。因为并发操作,多个用户同时访问同一个数据。可能引发并发访问的问题:

6.3MySQL数据库有四种隔离级别

mysql 事务里面能套事务吗 mysql 事务嵌套_数据_06

 

隔离级别越高,性能越差,安全性越高。

6.4MySQL事务隔离级别相关的命令

查询全局事务隔离级别

查询隔离级别
select @@tx_isolation;

设置事务隔离级别,需要退出MySQL再重新登录才能看到隔离级别的变化

设置隔离级别
set global transaction isolation level 级别字符串;【read uncommitted;read committed;repeatable read;serializable】

  1. 读未提交 read uncommitted
  2. 读已提交 read committed
  3. 可重复读 repeatable read
  4. 串行化 serializable

6.5脏读的演示

    read uncommitted: 读未提交:事物A和事物B,事物A未提交的数据,事物B可以读取到。 这种隔离级别最低,这种级别一般是在理论上存在,数据库隔离级别一般都高于该级别。 三种并发问题都没解决。

1.打开A窗口登录MySQL,设置全局的隔离级别为read uncommitted。并且关闭自动提交

mysql> select @@tx_isolation;
+----------------+
| @@tx_isolation |
+----------------+
| READ-COMMITTED |
+----------------+
1 row in set, 1 warning (0.00 sec)

mysql> set global transaction isolation level read uncommitted;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@tx_isolation;
+----------------+
| @@tx_isolation |
+----------------+
| READ-COMMITTED |
+----------------+
1 row in set, 1 warning (0.00 sec)

mysql> exit
Bye
[root@harbor ~]# mysql -uroot -p123456aA
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 5.7.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select @@tx_isolation;
+------------------+
| @@tx_isolation   |
+------------------+
| READ-UNCOMMITTED |
+------------------+
1 row in set, 1 warning (0.00 sec)

mysql> set @@autocommit=0;
Query OK, 0 rows affected (0.00 sec)

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

2.打开B窗口,AB窗口都开启事务

A:  关闭自动提交commit,隔离级别是全局的read uncommitted ,也就是A这个开启的事务操作了数据,但是没有提交。在当前不提交且read uncommitted隔离级别时候,B窗口的事务也是可以读取到没有提交的数据。这就是脏读

mysql> set @@autocommit=0;
Query OK, 0 rows affected (0.00 sec)

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

mysql> use wubo
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> update user1 set address="shanghai" where id=14;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

B:开启事务,关闭自动提交,查看数据,能查看到A没有提交事务的数据。

mysql> set @@autocommit=0;
Query OK, 0 rows affected (0.00 sec)

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

mysql> use wubo;
Database changed
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from user1 where id=14;
+----+--------+------+----------+
| id | name   | age  | address  |
+----+--------+------+----------+
| 14 | wubo11 |  111 | shanghai |
+----+--------+------+----------+
1 row in set (0.00 sec)

A:回滚

mysql> rollback;

B:也能看到回滚后的信息。

mysql> select * from user1 where id=14;
+----+--------+------+----------+
| id | name   | age  | address  |
+----+--------+------+----------+
| 14 | wubo11 |  111 | wubo     |
+----+--------+------+----------+
1 row in set (0.00 sec)

脏读非常危险的,比如张三向李四购买商品,张三开启事务,向李四账号转入500块,然后打电话给李四说钱已经转了。李四一查询钱到账了,发货给张三。张三收到货后回滚事务,李四的再查看钱没了。

解决脏读的问题:将全局的隔离级别进行提升

   read committed:读已提交:事务A只能读取到事务B提交的数据,这种级别可以避免“脏数据” ,这种隔离级别会导致“不可重复读取” ,Oracle默认隔离级别
1.在A窗口设置全局的隔离级别为read committed,然后退出重新进入

mysql> set global transaction isolation level read committed;
mysql> select @@tx_isolation;
+----------------+
| @@tx_isolation |
+----------------+
| READ-COMMITTED |
+----------------+
1 row in set, 1 warning (0.00 sec)

 

B窗口退出MySQL,B窗口再进入MySQL

AB窗口同时开启事务且关闭自动提交事务commit,此时AB数据是一样的

mysql> set @@autocommit=0;
Query OK, 0 rows affected (0.00 sec)

mysql> start transaction;

mysql> select * from user1 where id=14;
+----+--------+------+----------+
| id | name   | age  | address  |
+----+--------+------+----------+
| 14 | wubo11 |  111 | shanghai |
+----+--------+------+----------+
1 row in set (0.00 sec)

 

2.A更新数据,未提交

mysql> update user1 set address="wubo" where id=14;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from user1 where id=14;
+----+--------+------+---------+
| id | name   | age  | address |
+----+--------+------+---------+
| 14 | wubo11 |  111 | wubo    |
+----+--------+------+---------+
1 row in set (0.00 sec)

3.B窗口查询

mysql> select * from user1 where id=14;
+----+--------+------+----------+
| id | name   | age  | address  |
+----+--------+------+----------+
| 14 | wubo11 |  111 | shanghai |
+----+--------+------+----------+
1 row in set (0.00 sec)

A窗口commit提交事务

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

mysql> select * from user1 where id=14;
+----+--------+------+---------+
| id | name   | age  | address |
+----+--------+------+---------+
| 14 | wubo11 |  111 | wubo    |
+----+--------+------+---------+
1 row in set (0.01 sec)

4.B窗口查看账户

mysql> select * from user1 where id=14;
+----+--------+------+---------+
| id | name   | age  | address |
+----+--------+------+---------+
| 14 | wubo11 |  111 | wubo    |
+----+--------+------+---------+
1 row in set (0.00 sec)

结论:read committed的方式可以避免脏读的发生,A不提交事务,B是无法获取A数据的。没有提交事务A的数据此时在事务日志里面,不在表中,只有commit了才会同步到表中。而B是从表中获取数据当然也会通过自己的事务日志处理之后返回B。但是解决不了不可重复读和幻读

6.6不可重复读的演示

A和B

   repeatable read:可重复读:- 事务A和事务B,事务A提交之后的数据,事务B读取不到 - 事务B是可重复读取数据 - 这种隔离级别高于读已提交 - 换句话说,对方提交之后的数据,我还是读取不到 - 这种隔离级别可以避免“不可重复读取”,达到可重复读取 - 比如1点和2点读到数据是同一个 - MySQL默认级别 - 虽然可以达到可重复读取,但是会导致“幻像读”

mysql> select @@tx_isolation;
+----------------+
| @@tx_isolation |
+----------------+
| READ-COMMITTED |
+----------------+
1 row in set, 1 warning (0.00 sec)

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

1.开启A和B窗口开启事务

B 查看数据  ,此时A还没有跟新数据,就是A更新数据只要不提交事务这里也是看不到新数据的。只有A提交了事务这里才可以看到新数据,即使B不提交事务也能看到。这就是在B这个一个事务中读取两次结果不同,就是不可重复读

mysql> start transaction;
mysql> select * from user1 where id=14;
+----+--------+------+---------+
| id | name   | age  | address |
+----+--------+------+---------+
| 14 | wubo11 |  111 | wubo    |
+----+--------+------+---------+
1 row in set (0.00 sec)

2 A 跟新数据,且提交事务

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

mysql> update user1 set address="beijing" where id=14;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

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

   3. 再次查看B,此时B没有提交事务。

mysql> select * from user1 where id=14;
+----+--------+------+---------+
| id | name   | age  | address |
+----+--------+------+---------+
| 14 | wubo11 |  111 | beijing |
+----+--------+------+---------+
1 row in set (0.00 sec)

两次查询输出的结果不同,到底哪次是对的?不知道以哪次为准。 很多人认为这种情况就对了,无须困惑,当然是后面的为准。我们可以考虑这样一种情况,比如银行程序需要将查询结果分别输出到电脑屏幕和发短信给客户,结果在一个事务中针对不同的输出目的地进行的两次查询不一致,导致文件和屏幕中的结果不一致,银行工作人员就不知道以哪个为准了。
解决不可重复读的问题
将全局的隔离级别进行提升为:repeatable read

1.A窗口设置隔离级别为:repeatable read

mysql> set global transaction isolation level repeatable read;
mysql> exit
Bye
[root@harbor ~]# mysql -uroot -p123456aA
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 36
Server version: 5.7.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use wubo
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user1 set address="beijing" where id=14;^C
mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation  |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set, 1 warning (0.01 sec)

2.A和B取消自动提交事务,且都开启事务

mysql> set @@autocommit=0;
Query OK, 0 rows affected (0.00 sec)

3.B窗口退出MySQL,B窗口再进入MySQL

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

mysql> select * from user1 where id=14;
+----+--------+------+---------+
| id | name   | age  | address |
+----+--------+------+---------+
| 14 | wubo11 |  111 | beijing |
+----+--------+------+---------+
1 row in set (0.00 sec)

4.A窗口更新数据,A提交事务或不提交事务,B【只要B不提交事务】都是看不到新数据的

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

mysql> update user1 set address="shanghai" where id=14;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> commit;
Query OK, 0 rows affected (0.00 sec)
#A中的数据
mysql> select * from user1 where id=14;
+----+--------+------+----------+
| id | name   | age  | address  |
+----+--------+------+----------+
| 14 | wubo11 |  111 | shanghai |
+----+--------+------+----------+
1 row in set (0.00 sec)

5.B窗口查询,B中的数据。发现A和B数据不一样

mysql> select * from user1 where id=14;
+----+--------+------+---------+
| id | name   | age  | address |
+----+--------+------+---------+
| 14 | wubo11 |  111 | beijing |
+----+--------+------+---------+
1 row in set (0.00 sec)

B提交事务,此时A和B数据一致了

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

mysql> select * from user1 where id=14;
+----+--------+------+----------+
| id | name   | age  | address  |
+----+--------+------+----------+
| 14 | wubo11 |  111 | shanghai |
+----+--------+------+----------+
1 row in set (0.00 sec)

结论:同一个事务中为了保证多次查询数据一致,必须使用repeatable read隔离级别

6.7幻读的演示

在MySQL中无法看到幻读的效果。

serializable:串行化:事务A和事务B,事务A在操作数据库时,事务B只能排队等待 这种隔离级别很少使用,吞吐量太低,用户体验差 这种级别可以避免“幻像读”,每一次读取的都是数据库中真实存在数据,事务A与事务B串行, 而不并发
1.开启A窗口

set global transaction isolation level serializable; – 设置隔离级别为最高

mysql> set global transaction isolation level serializable;

exit
[root@harbor ~]# mysql -uroot -p123456aA
mysql> select @@tx_isolation;
+----------------+
| @@tx_isolation |
+----------------+
| SERIALIZABLE   |
+----------------+
1 row in set, 1 warning (0.00 sec)

2.A窗口退出MySQL,A窗口重新登录MySQL

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

mysql> 
mysql> select count(*) from user1;
+----------+
| count(*) |
+----------+
|       12 |
+----------+
1 row in set (0.00 sec)

3.再开启B窗口,登录MySQL

4.在B窗口中开启事务,添加一条记录

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

mysql> insert into user1(name,age,address) value("wubo12",222,"beijing12");

mysql 事务里面能套事务吗 mysql 事务嵌套_mysql_07

长时间: 

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

mysql> insert into user1(name,age,address) value("wubo12",222,"beijing12");
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

 5.在A窗口中commit提交事务,B窗口中insert语句会在A窗口事务提交后立马运行

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

B已经执行了 

mysql> insert into user1(name,age,address) value("wubo12",222,"beijing12");
Query OK, 1 row affected (5.37 sec)

mysql>

6 在A窗口中接着查询,发现执行不了。此时在B窗口commit之后A才可以查询,数据会发生变化

A:由于B没有提交导致A要等

mysql> select count(*) from user1;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

B:提交事务

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

A:才可以继续。因为serializable隔离级别是顺序的执行

mysql> select count(*) from user1;
+----------+
| count(*) |
+----------+
|       13 |
+----------+
1 row in set (0.00 sec)

结论:使用serializable隔离级别,一个事务没有执行完,其他事务的SQL执行不了,可以挡住幻读