当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL InnoDB之事务与锁详解

MySQL InnoDB之事务与锁详解

2017年12月12日  | 移动技术网IT编程  | 我要评论

引题:为何引入事务?

1>.数据完整性

2>.数据安全性

3>.充分利用系统资源,提高系统并发处理的能力

1. 事务的特征

事务具有四个特性:原子性(atomiocity)、一致性(consistency)、隔离性(isolation)和持久性(durability),这四个特性简称acid特性。

1.1原子性

事务是数据库的逻辑工作单位,事务中包括的所有操作要么都做,要么都不做。

1.2 一致性

事务执行的结果必须是使数据库从一个一致性的状态变到另外一个一致性状态。

1.3 隔离性

一个事务的执行不能被其他事务干扰。即一个事务内部的操作及使用的数据对其他

事务是隔离的,并发执行的各个事务之间互相不干扰。

1.4 持久性

一个事务一旦成功提交,对数据库中数据的修改就是持久性的。接下来其他的其他

操作或故障不应该对其执行结果有任何影响。

2. mysql的innodb引擎中事物与锁

2.1 select …… lock in share mode

会话事务中查找的数据,加上一个共享锁。若会话事务中查找的数据已经被其他会话事务加上独占锁的话,共享锁会等待其结束再加,若等待时间过长就会显示事务需要的锁等待超时。

2.2 select ….. for update

会话事务中查找的数据,加上一个读更新琐,其他会话事务将无法再加其他锁,必须等待其结束。

2.3 insert、update、delete

会话事务会对dml语句操作的数据加上一个独占锁,其他会话的事务都将会等待其释放独占锁。

2.4 gap and next key lock(间隙锁)

innodb引擎会自动给会话事务中的共享锁、更新琐以及独占锁,需要加到一个区间值域的时候,再加上个间隙锁(或称范围锁),对不存在的数据也锁住,防止出现幻写。

备注:

以上2.1,2.2,2.3,2.4中描述的情况,跟mysql所设置的事务隔离级别也有关系。

3.四种事务隔离模式

3.1 read uncommited

select的时候允许脏读,即select会读取其他事务修改而还没有提交的数据。

3.2 read commited

select的时候无法重复读,即同一个事务中两次执行同样的查询语句,若在第一次与第二次查询之间时间段,其他事务又刚好修改了其查询的数据且提交了,则两次读到的数据不一致。

3.3 repeatable read

select的时候可以重复读,即同一个事务中两次执行同样的查询语句,得到的数据始终都是一致的。

3.4 serializable

与可重复读的唯一区别是,默认把普通的select语句改成select …. lock in share mode。即为查询语句涉及到的数据加上共享琐,阻塞其他事务修改真实数据。

4. 验证事务与锁定示例

接下来,我们将以mysql中的innodb引擎,解释其如何实现acid特性,不同隔离级别下事务与事务之间的影响。示例表结构:

create table `account ` (

`id` int(11) not null auto_increment,

`vaccount_id` varchar(32) not null,

`gmt_create` datetime not null,

primary key (`id`),

key `idx_vaccount_parameter_vaccountid ` (`vaccount_id`)

) engine=innodb auto_increment=1 default charset=utf8 collate utf8_general_ci;

然后向表account中写入10w条创建日期分布合理的帐号数据,以方便测试之用。

tx_isolation:set global tx_isolation='read-uncommitted'
id 事务1 事务1输出 事务2 事务2输出
1 start transaction;      
2 select vaccount_id from account  where id =1001; caimao101510    
      start transaction;  
3     update account set vaccount_id='uncommitted' where id =1001;  
4     select vaccount_id from account  where id =1001; uncommitted
5 select vaccount_id from account  where id =1001; uncommitted    
6     rollback;  
7 select vaccount_id from account  where id =1001; caimao101510    
8 commit;      
tx_isolation:set global tx_isolation='read-committed'
id 事务1 事务1输出 事务2 事务2输出
1 start transaction;      
2 select vaccount_id from account  where id =1001; caimao101510    
3     start transaction;  
4     update account set vaccount_id='uncommitted' where id =1001;  
5     select vaccount_id from account  where id =1001; uncommitted
6 select vaccount_id from account  where id =1001; caimao101510    
7     commit;  
8 select vaccount_id from account  where id =1001; uncommitted    
9 commit;      
tx_isolation:set global tx_isolation='repeatable-read'
id 事务1 事务1输出 事务2 事务2输出
1 start transaction;      
2 select vaccount_id from account  where id =1001; caimao101510    
3     start transaction;  
4     update account set vaccount_id='uncommitted' where id =1001;  
5     select vaccount_id from account  where id =1001; uncommitted
6 select vaccount_id from account  where id =1001; caimao101510    
7     commit;  
8 select vaccount_id from account  where id =1001; caimao101510    
9 commit;      
tx_isolation:set global tx_isolation='serializable'
id 事务1 事务1输出 事务2 事务2输出
1 start transaction;      
2 select vaccount_id from account  where id =1001; caimao101510    
3     start transaction;  
4     update account set vaccount_id='uncommitted' where id =1001; state: updating
5 select vaccount_id from account  where id =1001; caimao101510    
      事务2超时  
6 commit;      
7 start transaction;      
8 update account set vaccount_id='uncommitted' where id =1001;      
9     start transaction;  
10     select vaccount_id from account  where id =1001; state:statistics
11     事务2超时  
12 commit;      
tx_isolation:set global tx_isolation='repeatable-read'
id 事务1 事务1输出 事务2 事务2输出
1 start transaction;      
2 select max(id) from account; 124999    
3     start transaction;  
4 update account set gmt_create=date_add(gmt_create,interval +1 day) where id >=124999;      
5     insert into account(vaccount_id,gmt_create) values(‘eugene',now()); state:update
6     事务2超时  
7     start transaction;  
8     select * from account where id =124998; 2007-10-20 13:47
9     update account set gmt_create=date_add(gmt_create,interval +1 day) where id =124998; 执行成功
10     select * from account where id =124998; 2007-10-21 13:47
11 commit;      
12     commit;  
         
1 start transaction;      
2 update account set gmt_create=date_add(gmt_create,interval -1 day) where gmt_create >'2009-07-01′;      
3     start transaction;  
4     select * from account where gmt_create>'2009-07-10′ limit 1; 2009-10-2 13:47
5 select * from account where gmt_create>'2009-07-10′ limit 1; 2009-10-1 13:47   state:update
6     insert into account(vaccount_id,gmt_create) values(‘gmt_create_test',now());  
7     事务2超时  
8 commit;      
9     select * from account where gmt_create>'2009-07-10′ limit 1; 2009-10-1 13:47
无索引条件更新事务
1 start transaction;      
  update account set gmt_create=date_add(gmt_create,interval -1 day) where gmt_create >'2009-07-01′ and gmt_create <'2009-07-10′;      
      start transaction;  
      insert into account(vaccount_id,gmt_create) values(‘gmt_create_interval',now());  
      事务2超时  
  commit;

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网