事务处理
事务处理是一种机制,用来管理必须成批执行的MySQL操作,以保证数据库不包含不完整的操作结果。利用事务处理,可以保证一组操作不会中途停止,它们或者作为整体执行,或者完全不执行(除非明确指示)。如果没有错误发生,整组语句提交给(写到)数据库表。如果发生错误,则进行回退(撤销)以恢复数据库到某个已知且安全的状态。
MyISAM和InnoDB是两种支持事务的最常用引擎。前者不支持明确的事务处理管理,而后者支持。
- 事务(transaction):一条或一组SQL语句,一般指insert、update、delete语句
- 开启事务(start transaction):开始一个事务
- 提交(commit):指将未存储的sql语句结果写入数据库表
- 回滚(rollback):指撤销指定SQL语句的过程
- 保留点(savepoint):指事务处理中设置的临时占位符,你可以对它发布回退,来达成部分回退的目的
MySQL默认开启自动提交模式,即每一条DML语句就是一个事务,若执行成功,则自动提交;若执行失败,则自动回滚
自动提交模式可以设置开关
set autocommit = off;
set autocommit = on;
show variables like '%auto%'; #查看状态
1、事务的提交与回滚
MariaDB [bjpowernode]> start transaction;
Query OK, 0 rows affected (0.000 sec)
MariaDB [bjpowernode]> select * from dept;
+--------+-------------+----------+
| deptno | dname | loc |
+--------+-------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCHING | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+-------------+----------+
4 rows in set (0.000 sec)
MariaDB [bjpowernode]> update dept set loc='London' where deptno=10;
Query OK, 1 row affected (0.000 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [bjpowernode]> select * from dept;
+--------+-------------+---------+
| deptno | dname | loc |
+--------+-------------+---------+
| 10 | ACCOUNTING | London |
| 20 | RESEARCHING | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+-------------+---------+
4 rows in set (0.000 sec)
MariaDB [bjpowernode]> update dept set loc='London' where deptno=20;
Query OK, 1 row affected (0.000 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [bjpowernode]> select * from dept;
+--------+-------------+---------+
| deptno | dname | loc |
+--------+-------------+---------+
| 10 | ACCOUNTING | London |
| 20 | RESEARCHING | London |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+-------------+---------+
4 rows in set (0.000 sec)
MariaDB [bjpowernode]> rollback;
Query OK, 0 rows affected (0.005 sec)
MariaDB [bjpowernode]> select * from dept;
+--------+-------------+----------+
| deptno | dname | loc |
+--------+-------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCHING | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+-------------+----------+
4 rows in set (0.000 sec)
2、使用保留点
MariaDB [bjpowernode]> start transaction;
Query OK, 0 rows affected (0.000 sec)
MariaDB [bjpowernode]> select * from dept;
+--------+-------------+----------+
| deptno | dname | loc |
+--------+-------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCHING | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+-------------+----------+
4 rows in set (0.000 sec)
MariaDB [bjpowernode]> update dept set loc='LONDON' where deptno=10;
Query OK, 1 row affected (0.000 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [bjpowernode]> select * from dept;
+--------+-------------+---------+
| deptno | dname | loc |
+--------+-------------+---------+
| 10 | ACCOUNTING | LONDON |
| 20 | RESEARCHING | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+-------------+---------+
4 rows in set (0.000 sec)
MariaDB [bjpowernode]> savepoint P1;
Query OK, 0 rows affected (0.000 sec)
MariaDB [bjpowernode]> update dept set loc='LONDON' where deptno=20;
Query OK, 1 row affected (0.000 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [bjpowernode]> select * from dept;
+--------+-------------+---------+
| deptno | dname | loc |
+--------+-------------+---------+
| 10 | ACCOUNTING | LONDON |
| 20 | RESEARCHING | LONDON |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+-------------+---------+
4 rows in set (0.000 sec)
MariaDB [bjpowernode]> rollback to P1;
Query OK, 0 rows affected (0.000 sec)
MariaDB [bjpowernode]> select * from dept;
+--------+-------------+---------+
| deptno | dname | loc |
+--------+-------------+---------+
| 10 | ACCOUNTING | LONDON |
| 20 | RESEARCHING | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+-------------+---------+
4 rows in set (0.000 sec)
MariaDB [bjpowernode]> rollback;
Query OK, 0 rows affected (0.003 sec)
MariaDB [bjpowernode]> select * from dept;
+--------+-------------+----------+
| deptno | dname | loc |
+--------+-------------+----------+
| 10 | ACCOUNTING | NEW YORK |
| 20 | RESEARCHING | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+--------+-------------+----------+
4 rows in set (0.000 sec)
3、事务的4个特征
事务具有四个特征,合成ACID
- 原子性(Atomicity)
整个事务中的所有操作,必须作为一个单元全部完成(或全部消失) - 一致性(Consistency)
在事务开始之前与结束之后,数据库都保持一致状态 - 隔离性(Isolation)
一个事务不会影响其他事务的运行 - 持久性(Durability)
在事务完成以后,该事务对数据库所作的更改将持久地保存在数据库之中,并不会被回滚
4、事务的隔离级别
事务的隔离级别决定了事务之间可见的级别
当多个客户端并发地访问同一个表时,可能出现下面的一致性问题
- 脏读取(Dirdy Read)
一个事务开始读取了某行数据,但是另外一个事务已经更新了此数据但没有能够及时提交,这就出现了脏读取 - 不可重复读(Non-repeatable Read)
在同一个事务中,同一个读操作对同一个数据的前后两次读取产生了不同的结果,这就是不可重复读 - 幻像读(Phantom Read)
幻像读时指在同一个事务中以前没有的行,由于其他事务的提交而出现的新行
InnoDB实现了四个隔离级别,用以控制事务所作的修改,并将修改通告至其他事务
- 读未提交(READ UNCOMMITTED)
允许一个事务可以看到其他事务未提交的修改 - 读已提交(READ COMMITTED)
允许一个事务只能看到其他事务已经提交的修改,未提交的修改是不可见的 - 可重复读(REPEATABLE READ)
确保如果在一个事务中执行两次相同的SELECT语句,都能得到相同的结果,不管其他事务是否提交这些修改
是InnoDB的缺省设置 - 串行化(SERIALIZABLE)
将一个事务与其他事务完全地隔离
隔离级别 | 脏读取 | 不可重复读 | 幻像读 |
---|---|---|---|
读未提交 | 可能 | 可能 | 可能 |
读已提交 | 不可能 | 可能 | 可能 |
可重复读 | 不可能 | 不可能 | 对InnoDB不可能 |
串行化 | 不可能 | 不可能 | 不可能 |
事务隔离级别的作用范围分为两种:
- 全局级(GLOBAL):对所有的会话有效
- 会话级(SESSION):只对当前的会话有效
可以在my.ini
文件中使用transaction-isolation选项来设置服务器的缺省隔离级别
- READ-UNCOMMITTED
- READ-COMMITTED
- REPEATABLE-READ
- SERIALIZABLE
[mysqld]
transaction-isolation = READ-COMMITTED;
也可以动态设置隔离级别
SET [GLOBAL | SESSION] TRANSACTION ISOLATION LEVEL ;
查看隔离级别
#会话级
SELECT @@tx_isolation;
SELECT @@session.tx_isolation;
#全局级
SELECT @@global.tx_isolation;
5、实例说明隔离级别
5.1、read uncommitted(未提交读) --脏读(Drity Read)
会话1 | 会话2 |
---|---|
set global transaction isolation level read uncommitted; | |
重新登陆会话 | 重新登陆会话 |
use bjpowernode; | use bjpowernode; |
create table tx(id int(11),num int(10)); | |
start transaction; | |
start transaction; | |
insert into tx values (1,10); | |
select * from tx; | |
rollback; | |
select * from tx; |
5.2、read committed(已提交读)
会话1 | 会话2 |
---|---|
set global transaction isolation level read committed; | |
重新登陆会话 | 重新登陆会话 |
start transaction; | |
start transaction; | |
insert into tx values (1,10); | |
select * from tx; | |
select * from tx; | |
commit; | |
select * from tx; |
5.3、repeatable read(可重复读)
会话1 | 会话2 |
---|---|
set global transaction isolation level repeatable read; | |
重新登陆会话 | 重新登陆会话 |
start transaction; | |
start transaction; | |
select * from tx; | |
insert into tx values (1,10); | |
select * from tx; | |
commit; | |
select * from tx; |