CURD不加控制的时候,会有什么问题呢?
上层看起来比较简单的需求,可能对应后端要做很多的工作,组合起来才是一个完整的需求解决方案.
一个整体,要么不做,要么做完(绝对成功,绝对失败),不要出现中间操作这样的概念----原子性!!!
上面就称为一个事务
!!! 即就是一个或者多个sql语句的集合
事务本身不是数据库软件天然有的,事务本质工作其实是为了简化程序猿的工作模型!
CURD满足什么属性,才能解决上述问题?
1.买票的过程得是原子的吧.
2.买票互相应该不能影响吧.
3.买完票应该要永久有效吧.
4.买前和买后都要是确定的状态吧.
正文开始!!!
事务就是一组DML语句组成,这些语句在逻辑上存在相关性,这一组DML语句要么全部成功,要么全部失败,是一个整体.MySQL提供一种机制,保证我们达到这样的效果.事务还规定不同的客户端看到的数据是不相同的.
事务就是要做的或所做的事情,主要用于处理操作量大,复杂度高的数据.假设一种场景:你毕业了,学校的教务系统后台MySQL中,不在需要你的数据,要删除你的所有信息(一般不会),那么要删除你的基本信息(姓名,电话,籍贯等)的同时,也删除和你有关的其他信息.比如:你的各科成绩,你在校表现,甚至你在论坛发过的文章等.这样,就需要多态MySQL语句构成,那么所有这些操作合起来,就构成了一个事务.
正如上面所说,一个MySQL数据库,可不止一个事务在运行,同一时刻,甚至有大量的请求被包装成事务,再向MySQL服务器发起事务处理请求.而每条事务至少一条SQL,最多很多SQL,这样如果大家都访问同样的表数据,在不加保护的情况,就绝对会出现问题.甚至,因为事务有多条SQL构成,那么,也会存在执行到一半出错或者不想在执行的情况,那么已经执行的怎么办呢?
所以,一个完整的事务,绝对不是简单的sql集合,还需要满足如下四个属性:
Read uncommitted
),读提交(Read committed
),可重复读(repeatable read
)和串行化(Serializable
).上面四个属性,可以简称为ACID
.
原子性:(Atomicity,或称为不可分割性).
一致性:(Consistency)
隔离性:(Isolation,又称独立性)
持久性:(Durability)
事务被MySQL编写者设计出来,本质是为了当应用程序访问数据库的时候,事务能够简化我们的编程模型,不需要我们去考虑各种各样的潜在错误和并发问题.可以想一下当我们使用事务的时候,要么提交,要么回滚,我们不会去考虑网络异常了,服务器宕机了,同时更改一个数据怎么办等等…
**因此事务本质上是为了应用层服务的.**而不是伴随着数据库天生就有的.
备注:我们把MySQL中的一行信息,称为一条记录.
在MySQL中只有使用了InnoDB
数据库引擎的数据库或表才支持事务,MyISAM
不支持.
查看数据库引擎
mysql> show engines -- 表格显示
mysql> show engines\G -- 行显示
*************************** 1. row ***************************
Engine: InnoDB -- 引擎名称
Support: DEFAULT -- 默认引擎
Comment: Supports transactions, row-level locking, and foreign keys
Transactions: YES -- 支持事务
XA: YES
Savepoints: YES -- 支持事务保存节点
*************************** 2. row ***************************
Engine: MRG_MYISAM
Support: YES
Comment: Collection of identical MyISAM tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 3. row ***************************
Engine: MEMORY -- 内存引擎
Support: YES
Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 4. row ***************************
Engine: BLACKHOLE
Support: YES
Comment: /dev/null storage engine (anything you write to it disappears)
Transactions: NO
XA: NO
Savepoints: NO
*************************** 5. row ***************************
Engine: MyISAM
Support: YES
Comment: MyISAM storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 6. row ***************************
Engine: CSV
Support: YES
Comment: CSV storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 7. row ***************************
Engine: ARCHIVE
Support: YES
Comment: Archive storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 8. row ***************************
Engine: PERFORMANCE_SCHEMA
Support: YES
Comment: Performance Schema
Transactions: NO
XA: NO
Savepoints: NO
*************************** 9. row ***************************
Engine: FEDERATED
Support: NO
Comment: Federated MySQL storage engine
Transactions: NULL
XA: NULL
Savepoints: NULL
9 rows in set (0.00 sec)
事务的提交方式常见的有两种:
查看事务提交方式
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
1 row in set (0.00 sec)
用set来改变MySQL的自动提交模式:
mysql> set autocommit=0; --- set autocommit=0 禁止自动提交
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | OFF |
+---------------+-------+
1 row in set (0.01 sec)
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | OFF |
+---------------+-------+
1 row in set (0.01 sec)
mysql> set autocommit=1; --- set autocommit=1 开启自动提交
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
1 row in set (0.01 sec)
mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set, 1 warning (0.00 sec)
## 为了演示,我将MySQL的默认隔离级别设置成读未提交.
mysql> set global transaction isolation level read uncommitted;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set, 1 warning (0.00 sec)
# 需要重启终端进行查看
mysql> quit
Bye
[root@centos7 2023_05_07]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 53
Server version: 5.7.41 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, 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> create table if not exists emp(
-> id int primary key,
-> name varchar(32) not null,
-> set decimal(10,2) not null default 0.0
-> )engine=innodb;
Query OK, 0 rows affected (0.01 sec)
## 查看事务是否自动提交.我们故意设置成自动提交,看看该选项是否影响begin.
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
1 row in set (0.00 sec)
mysql> start transaction; -- 开始一个事务begin也可以,推荐begin
Query OK, 0 rows affected (0.00 sec)
mysql> savepoint save1; -- 创建一个保存点save1
Query OK, 0 rows affected (0.00 sec)
mysql> insert into emp values (1, '胡强', 10000.00); -- 插入一条记录
Query OK, 1 row affected (0.05 sec)
mysql> savepoint save2; -- 创建一个保存点save2
Query OK, 0 rows affected (0.01 sec)
mysql> insert into emp values (2, '欧阳莫非', 9000.00); -- 在插入一条记录
Query OK, 1 row affected (0.00 sec)
mysql> select * from account; -- 两条记录都在了
+----+--------+----------+
| id | name | blance |
+----+--------+----------+
| 1 | 胡强 | 100.00 |
| 2 | 欧阳莫非 | 10000.00 |
+----+--------+----------+
2 rows in set (0.00 sec)
mysql> rollback to save2; -- 回滚到保存点save2
Query OK, 0 rows affected (0.03 sec)
mysql> select * from account; -- 一条记录没有了
+----+--------+--------+
| id | name | blance |
+----+--------+--------+
| 1 | 胡强 | 100.00 |
+----+--------+--------+
1 row in set (0.00 sec)
mysql> rollback; -- 直接rollback,回滚在最开始
Query OK, 0 rows affected (0.00 sec)
mysql> select * from account; -- 所有刚刚的记录没有了
Empty set (0.00 sec)
# 终端A
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into emp values (1,'胡强',10000.00);
Query OK, 1 row affected (0.00 sec)
mysql> insert into emp values (2,'欧阳莫非',9000.00);
Query OK, 1 row affected (0.00 sec)
mysql> Aborted
## 终端B
mysql> select * from emp;
+----+--------------+----------+
| id | name | sal |
+----+--------------+----------+
| 1 | 胡强 | 10000.00 |
| 2 | 欧阳莫非 | 9000.00 |
+----+--------------+----------+
2 rows in set (0.00 sec)
mysql> select * from emp;
Empty set (0.00 sec)
## 终端
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into emp values (1,'胡强',10000.00);
Query OK, 1 row affected (0.00 sec)
mysql> insert into emp values (2,'欧阳莫非',9000.00);
Query OK, 1 row affected (0.01 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> Aborted
## 终端B
mysql> select * from emp;
Empty set (0.00 sec)
mysql> select * from emp;
+----+--------------+----------+
| id | name | sal |
+----+--------------+----------+
| 1 | 胡强 | 10000.00 |
| 2 | 欧阳莫非 | 9000.00 |
+----+--------------+----------+
2 rows in set (0.00 sec)
mysql> select * from emp;
+----+--------------+----------+
| id | name | sal |
+----+--------------+----------+
| 1 | 胡强 | 10000.00 |
| 2 | 欧阳莫非 | 9000.00 |
+----+--------------+----------+
2 rows in set (0.00 sec)
-- 终端 A
mysql> select *from emp; --查看历史数据
+----+--------+--------+
| id | name | blance |
+----+--------+--------+
| 1 | 张三 | 100.00 |
+----+--------+--------+
1 row in set (0.00 sec)
mysql> show variables like 'autocommit'; --查看事务提交方式
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
1 row in set (0.00 sec)
mysql> set autocommit=0; --关闭自动提交
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'autocommit'; --查看关闭之后结果
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | OFF |
+---------------+-------+
1 row in set (0.00 sec)
mysql> begin; --开启事务
Query OK, 0 rows affected (0.00 sec)
mysql> insert into emp values (2, '李四', 10000); --插入记录
Query OK, 1 row affected (0.00 sec)
mysql> select *from emp; --查看插入记录,同时查看终端B
+----+--------+----------+
| id | name | blance |
+----+--------+----------+
| 1 | 张三 | 100.00 |
| 2 | 李四 | 10000.00 |
+----+--------+----------+
2 rows in set (0.00 sec)
mysql> Aborted --再次异常终止
-- 终端B
mysql> select * from emp; --终端A崩溃前
+----+--------+----------+
| id | name | blance |
+----+--------+----------+
| 1 | 张三 | 100.00 |
| 2 | 李四 | 10000.00 |
+----+--------+----------+
2 rows in set (0.00 sec)
mysql> select * from emp; --终端A崩溃后,自动回滚
+----+--------+--------+
| id | name | blance |
+----+--------+--------+
| 1 | 张三 | 100.00 |
+----+--------+--------+
1 row in set (0.00 sec)
autocommit是否是自动提交,并不影响用户手动开启事务!!!
## 实验一
-- 终端A
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
1 row in set (0.00 sec)
mysql> insert into emp values (3,'苏克杰',8000.00);
Query OK, 1 row affected (0.01 sec)
mysql> insert into emp values (4,'庞小白',8000.00);
Query OK, 1 row affected (0.00 sec)
mysql> quit
Bye
mysql> select * from emp;
Empty set (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 3 | 苏克杰 | 8000.00 |
| 4 | 庞小白 | 8000.00 |
+----+-----------+---------+
2 rows in set (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 3 | 苏克杰 | 8000.00 |
| 4 | 庞小白 | 8000.00 |
+----+-----------+---------+
2 rows in set (0.00 sec)
## 实验二
mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | OFF |
+---------------+-------+
1 row in set (0.00 sec)
mysql> insert into emp values (5,'唐海星',8500.00);
Query OK, 1 row affected (0.00 sec)
mysql> insert into emp values (6,'金若愚',8500.00);
Query OK, 1 row affected (0.00 sec)
mysql> quit
Bye
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 3 | 苏克杰 | 8000.00 |
| 4 | 庞小白 | 8000.00 |
+----+-----------+---------+
2 rows in set (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 3 | 苏克杰 | 8000.00 |
| 4 | 庞小白 | 8000.00 |
| 5 | 唐海星 | 8500.00 |
| 6 | 金若愚 | 8500.00 |
+----+-----------+---------+
4 rows in set (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 3 | 苏克杰 | 8000.00 |
| 4 | 庞小白 | 8000.00 |
+----+-----------+---------+
2 rows in set (0.00 sec)
## 实验三
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | OFF |
+---------------+-------+
1 row in set (0.01 sec)
mysql> insert into emp values (5,'唐海星',8500.00);
Query OK, 1 row affected (0.00 sec)
mysql> insert into emp values (6,'金若愚',8500.00);
Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> quit --终端A退出后,并不影响,已经持久化。autocommit
Bye
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 3 | 苏克杰 | 8000.00 |
| 4 | 庞小白 | 8000.00 |
+----+-----------+---------+
2 rows in set (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 3 | 苏克杰 | 8000.00 |
| 4 | 庞小白 | 8000.00 |
| 5 | 唐海星 | 8500.00 |
| 6 | 金若愚 | 8500.00 |
+----+-----------+---------+
4 rows in set (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 3 | 苏克杰 | 8000.00 |
| 4 | 庞小白 | 8000.00 |
| 5 | 唐海星 | 8500.00 |
| 6 | 金若愚 | 8500.00 |
+----+-----------+---------+
4 rows in set (0.01 sec)
1.只要输入begin或者start transaction,事务便必须要通过commit提交,才会持久化,与是否设置set autocommit无关.
2.事务可以手动回滚.同时,当操作异常,MySQL会自动回滚.
3.对于InnoDB每一条SQL语言都默认封装成事务,自动提交.(select有特殊情况,因为MySQL有MVCC).
4.从上面的例子,我们能看到事务本身的原子性(回滚),持久性(commit).
5.那么隔离性?一致性?
事务操作注意事项:
**隔离级别就是在隔离事务这件事情上面,做的不同程度的拖鞋.前提是依旧要保证数据安全.**即就是在效率和可靠性中找平衡.
隔离级别如何实现:隔离,基本都是通过锁实现的,不同的隔离级别,锁的使用是不同的.常见的有:表锁,行锁,读锁,写锁,间隙锁(GAP),Next-Key锁(GAP+行锁)等.
-- 查看
mysql> SELECT @@global.tx_isolation; --查看全局隔级别
+-----------------------+
| @@global.tx_isolation |
+-----------------------+
| REPEATABLE-READ |
+-----------------------+
1 row in set, 1 warning (0.00 sec)
mysql> SELECT @@session.tx_isolation; --查看会话(当前)全局隔级别
+------------------------+
| @@session.tx_isolation |
+------------------------+
| REPEATABLE-READ |
+------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> SELECT @@tx_isolation; --默认同上
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set, 1 warning (0.00 sec)
--设置
-- 设置当前会话 or 全局隔离级别语法
SET [SESSION | GLOBAL] TRANSACTION ISOLATION LEVEL {READ UNCOMMITTED | READ
COMMITTED | REPEATABLE READ | SERIALIZABLE}
--设置当前会话隔离性,另起一个会话,看不多,只影响当前会话
mysql> set session transaction isolation level serializable; -- 串行化
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @@global.tx_isolation; --全局隔离性还是RR
+-----------------------+
| @@global.tx_isolation |
+-----------------------+
| REPEATABLE-READ |
+-----------------------+
1 row in set, 1 warning (0.00 sec)
mysql> SELECT @@session.tx_isolation; --会话隔离性成为串行化
+------------------------+
| @@session.tx_isolation |
+------------------------+
| SERIALIZABLE |
+------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> SELECT @@tx_isolation; --同上
+----------------+
| @@tx_isolation |
+----------------+
| SERIALIZABLE |
+----------------+
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 @@global.tx_isolation;
+-----------------------+
| @@global.tx_isolation |
+-----------------------+
| READ-UNCOMMITTED |
+-----------------------+
1 row in set, 1 warning (0.00 sec)
mysql> SELECT @@session.tx_isolation;
+------------------------+
| @@session.tx_isolation |
+------------------------+
| READ-UNCOMMITTED |
+------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> SELECT @@tx_isolation;
+------------------+
| @@tx_isolation |
+------------------+
| READ-UNCOMMITTED |
+------------------+
1 row in set, 1 warning (0.00 sec)
-- 注意,如果没有现象,关闭mysql客户端,重新连接。
--几乎没有加锁,虽然效率高,但是问题太多,严重不建议采用
--终端A
-- 设置隔离级别为 读未提交
mysql> set global transaction isolation level read uncommitted;
Query OK, 0 rows affected (0.00 sec)
--重启客户端
mysql> select @@tx_isolation;
+------------------+
| @@tx_isolation |
+------------------+
| READ-UNCOMMITTED |
+------------------+
1 row in set, 1 warning (0.00 sec)
# 终端A
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from emp where id = 6;
Query OK, 1 row affected (0.00 sec)
--没有commit哦!!!
# 终端B
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 3 | 苏克杰 | 8000.00 |
| 4 | 庞小白 | 8000.00 |
| 5 | 唐海星 | 8500.00 |
| 6 | 金若愚 | 8500.00 |
+----+-----------+---------+
4 rows in set (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 3 | 苏克杰 | 8000.00 |
| 4 | 庞小白 | 8000.00 |
| 5 | 唐海星 | 8500.00 | --读到终端A更新但是未commit的数据[insert,delete同样]
+----+-----------+---------+
3 rows in set (0.00 sec)
## 一个事务在执行中,读到另一个事务的更新(或其他操作)但是为commit的数据,这种现象叫做脏读(dirty read).
mysql> select @@tx_isolation;
+----------------+
| @@tx_isolation |
+----------------+
| READ-COMMITTED |
+----------------+
1 row in set, 1 warning (0.01 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from emp where id=6;
Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
## 终端B
mysql> select @@tx_isolation;
+----------------+
| @@tx_isolation |
+----------------+
| READ-COMMITTED |
+----------------+
1 row in set, 1 warning (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 3 | 苏克杰 | 8000.00 |
| 4 | 庞小白 | 8000.00 |
| 5 | 唐海星 | 8500.00 |
| 6 | 金若愚 | 8500.00 |
+----+-----------+---------+
4 rows in set (0.00 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 3 | 苏克杰 | 8000.00 |
| 4 | 庞小白 | 8000.00 |
| 5 | 唐海星 | 8500.00 |
| 6 | 金若愚 | 8500.00 |
+----+-----------+---------+
4 rows in set (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 3 | 苏克杰 | 8000.00 |
| 4 | 庞小白 | 8000.00 |
| 5 | 唐海星 | 8500.00 |
+----+-----------+---------+
3 rows in set (0.00 sec)
## 读提交有问题吗???
## 不应该被正在和你一起运行的事务看到
## 这个下现象叫做不可重复读(non reapeatable raed)!!
## 这个现象是一个问题!!!
--终端A
mysql> set global transaction isolation level repeatable read; --设置全局隔离级别
RR
Query OK, 0 rows affected (0.01 sec)
--关闭终端重启
mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ | --隔离级别RR
+-----------------+
1 row in set, 1 warning (0.00 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from where id=3;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where id=3' at line 1
mysql> delete from emp where id=3;
Query OK, 1 row affected (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 4 | 庞小白 | 8000.00 |
| 5 | 唐海星 | 8500.00 |
+----+-----------+---------+
2 rows in set (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
## 终端B
mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set, 1 warning (0.00 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 3 | 苏克杰 | 8000.00 |
| 4 | 庞小白 | 8000.00 |
| 5 | 唐海星 | 8500.00 |
+----+-----------+---------+
3 rows in set (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 3 | 苏克杰 | 8000.00 |
| 4 | 庞小白 | 8000.00 |
| 5 | 唐海星 | 8500.00 |
+----+-----------+---------+
3 rows in set (0.00 sec)
## 实验二
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from emp where id=10;
Query OK, 1 row affected (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql>
## 终端B
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 4 | 庞小白 | 8000.00 |
| 5 | 唐海星 | 8500.00 |
| 10 | 沙乐乐 | 5000.00 |
+----+-----------+---------+
3 rows in set (0.00 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 4 | 庞小白 | 8000.00 |
| 5 | 唐海星 | 8500.00 |
+----+-----------+---------+
2 rows in set (0.00 sec)
## 可重复读本质其实就在一个事务内部,不受任何事务操作的影响,每次查到的数据都是一致的!!!
## 对所有操作全部加锁,进行串行化,不会有问题,但是只要串行化,效率很低,几乎完全不会被采用.
## 终端A
mysql> set global transaction isolation level serializable;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@tx_isolation;
+----------------+
| @@tx_isolation |
+----------------+
| SERIALIZABLE |
+----------------+
1 row in set, 1 warning (0.00 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 4 | 庞小白 | 8000.00 |
| 5 | 唐海星 | 8500.00 |
+----+-----------+---------+
2 rows in set (0.00 sec)
mysql> insert into emp values (10,'沙乐乐',5000.00);
## 终端B
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 4 | 庞小白 | 8000.00 |
| 5 | 唐海星 | 8500.00 |
+----+-----------+---------+
2 rows in set (0.00 sec)
--提交之后,终端A中的insert才会提交。
## 终端A
mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set, 1 warning (0.00 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 4 | 庞小白 | 8000.00 |
| 5 | 唐海星 | 8500.00 |
+----+-----------+---------+
2 rows in set (0.00 sec)
mysql> update emp set name='Lisa' where id=5;
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)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 4 | 庞小白 | 8000.00 |
| 5 | 袁周率 | 8500.00 |
+----+-----------+---------+
2 rows in set (0.00 sec)
## 终端B
mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set, 1 warning (0.00 sec)
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 4 | 庞小白 | 8000.00 |
| 5 | 唐海星 | 8500.00 |
+----+-----------+---------+
2 rows in set (0.00 sec)
mysql> update emp set name='袁周率' where id=5;
Query OK, 1 row affected (40.34 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from emp;
+----+-----------+---------+
| id | name | sal |
+----+-----------+---------+
| 4 | 庞小白 | 8000.00 |
| 5 | 袁周率 | 8500.00 |
+----+-----------+---------+
2 rows in set (0.00 sec)
现象结果是,update,insert,delete之间是会有加锁现象的,但是select和这些操作是不冲突的。这就是通过读写锁(锁有行锁或者表锁)+MVCC完成隔离性。
以上就是今天要讲的内容,下篇博客我来给大家基于多版本控制(MVCC)解决上述问题!