事务

1 定义:

如果一个包含多个步骤的业务操作,被事务管理,要么同时成功,要么同时失败。
1.1 举例


image.png

转账操作站在数据库的角度看有以上3步。在没有被事务管理时,如果第2步出现异常,可能会出现张三的账户少了500,李四的账号没收到这500。
被事务管理,开启事务后,如果在第2步出现了异常,则进行回滚,张三账户金额不变,提示你转账失败。如果顺利完成转账的3步,最后要提交事务,整个事务就完成了。

2 事务的操作

2.1 开启事务 :start transaction
2.2 回滚:rollback,回滚到开启事务的时候
2.3 提交事务:commit,结束事务
2.4 例子:
2.4.1 预置条件

--创建一个account表
create table account (id int primary key auto_increment,name varchar(20),balance decimal(10,2));
--插入数据
mysql> insert account (name,balance) values ('zhangsan',1000.00),('lisi',1000.00);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> select * from account;
+----+----------+---------+
| id | name     | balance |
+----+----------+---------+
|  1 | zhangsan | 1000.00 |
|  2 | lisi     | 1000.00 |
+----+----------+---------+
2 rows in set (0.00 sec)

2.4.1 没有设置事务,在从张三转500给李四的过程中出错。

---省略查看账户余额这一步
--从zhangsan的账户中减去500;
mysql> update account set balance = balance - 500.00 where name = 'zhangsan';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
---把lisi的名字写错成lishi,lisi的账户未增加500;
mysql> update account set balance = balance + 500.00 where name = 'lishi';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0
--张三的500块不见了
mysql> select * from account;
+----+----------+---------+
| id | name     | balance |
+----+----------+---------+
|  1 | zhangsan |  500.00 |
|  2 | lisi     | 1000.00 |
+----+----------+---------+
2 rows in set (0.00 sec)

mysql> 

2.4.2 设置事务,过程中发现出错,rollback会见状态还原到开启事务时。

mysql> select * from account;
+----+----------+---------+
| id | name     | balance |
+----+----------+---------+
|  1 | zhangsan | 1000.00 |
|  2 | lisi     | 1000.00 |
+----+----------+---------+
2 rows in set (0.00 sec)
---开启事务
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> update account set balance = balance - 500.00 where name = 'zhangsan';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
---发现操作出错,lisi的名字打错成lishi
mysql> update account set balance = balance + 500.00 where name = 'lishi';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0
----回滚,回到开启事务时的状态
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from account;
+----+----------+---------+
| id | name     | balance |
+----+----------+---------+
|  1 | zhangsan | 1000.00 |
|  2 | lisi     | 1000.00 |
+----+----------+---------+
2 rows in set (0.01 sec)

2.4.3 设置事务,过程中没有出错,操作完成后,进行提交,结束该事务。

mysql> update account set balance = balance - 500.00 where name = 'zhangsan';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update account set balance = balance + 500.00 where name = 'lisi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from account;
+----+----------+---------+
| id | name     | balance |
+----+----------+---------+
|  1 | zhangsan |  500.00 |
|  2 | lisi     | 1500.00 |
+----+----------+---------+
2 rows in set (0.00 sec)
--提交,结束事务
mysql> commit;
Query OK, 0 rows affected (0.01 sec)

你可能感兴趣的:(事务)