乐观锁
乐观锁与悲观锁不同的是,它是一种逻辑上的锁,而不需要数据库提供锁机制来支持
当数据很重要,回滚或重试一次需要很大的开销时,需要保证操作的ACID性质,此时应该采用悲观锁
而当数据对即时的一致性要求不高,重试一次不太影响整体性能时,可以采用乐观锁来保证最终一致性,同时有利于提高并发性
通常,乐观锁采用版本号/时间戳的形式实现:给数据额外增加一个版本号字段进行控制;更新时,若提交的数据所带的版本号与当前记录的版本号一致,则允许变更执行并更新版本号;若不一致,则意味着产生冲突,根据业务需求直接丢弃并返回失败,或者尝试合并
在MySQL的实践中,常见的一种使用乐观锁的方法,是在需要使用乐观锁的表中,新增一个version字段
例如:
create table product_amount (
id int not null primary key auto_increment,
product_name varchar(64) not null,
selling_amount int not null,
storing_amount int not null,
version int not null
);
当需要更新销售中的商品数量(selling_amount)时,使用如下的SQL语句:
update product_amount set selling_amount = #{selling_amount}, version = #{new_version} where id=#{id} and version = #{old_version};
若该语句返回1,则表示更新成功;若返回0,则表示前后的version不一致,产生冲突,更新失败
对于更新仓库中的商品数据(storing_amount)时,也是同理
不过,这样为每行记录都统一设置一个version字段的乐观锁方式,存在一个问题:上例中,如果同时需要单独对selling_amount及storing_amount进行update(两条SQL语句分别单独执行),那么后执行的一条会因为先执行的一条更新了version字段而失败,而这种失败显然是没有必要的,白白浪费了开销
一种比较好的方式是为每个需要乐观锁的字段单独设置版本号,例如对上例的改造:
create table product_amount (
id int not null primary key auto_increment,
product_name varchar(64) not null,
selling_amount int not null,
selling_version int not null,
storing_amount int not null,
storing_version int not null
);
selling_amount和storing_amount分别拥有自己的乐观锁版本号(selling_version和storing_version),更新时分别只关注自己的版本号,这样就不会因为版本号被其它字段修改而失败,提高了并发性
悲观锁实践:
悲观锁介绍(百科):
悲观锁,正如其名,它指的是对数据被外界(包括本系统当前的其他事务,以及来自外部系统的事务处理)修改持保守态度,因此,在整个数据处理过程中,将数据处于锁定状态。悲观锁的实现,往往依靠数据库提供的锁机制(也只有数据库层提供的锁机制才能真正保证数据访问的排他性,否则,即使在本系统中实现了加锁机制,也无法保证外部系统不会修改数据)。
使用场景举例:以MySQL InnoDB为例
商品goods表中有一个字段status,status为1代表商品未被下单,status为2代表商品已经被下单,那么我们对某个商品下单时必须确保该商品status为1。假设商品的id为1。
1如果不采用锁,那么操作方法如下:
//1.查询出商品信息
select status from t_goods where id=1;
//2.根据商品信息生成订单
insert into t_orders (id,goods_id) values (null,1);
//3.修改商品status为2
update t_goods set status=2;
上面这种场景在高并发访问的情况下很可能会出现问题。
前面已经提到,只有当goods status为1时才能对该商品下单,上面第一步操作中,查询出来的商品status为1。但是当我们执行第三步Update操作的时候,有可能出现其他人先一步对商品下单把goods status修改为2了,但是我们并不知道数据已经被修改了,这样就可能造成同一个商品被下单2次,使得数据不一致。所以说这种方式是不安全的。
2使用悲观锁来实现:
在上面的场景中,商品信息从查询出来到修改,中间有一个处理订单的过程,使用悲观锁的原理就是,当我们在查询出goods信息后就把当前的数据锁定,直到我们修改完毕后再解锁。那么在这个过程中,因为goods被锁定了,就不会出现有第三者来对其进行修改了。
注:要使用悲观锁,我们必须关闭mysql数据库的自动提交属性,因为MySQL默认使用autocommit模式,也就是说,当你执行一个更新操作后,MySQL会立刻将结果进行提交。
我们可以使用命令设置MySQL为非autocommit模式:
set autocommit=0;
设置完autocommit后,我们就可以执行我们的正常业务了。具体如下:
//0.开始事务
begin;/begin work;/start transaction; (三者选一就可以)
//1.查询出商品信息
select status from t_goods where id=1 for update;
//2.根据商品信息生成订单
insert into t_orders (id,goods_id) values (null,1);
//3.修改商品status为2
update t_goods set status=2;
//4.提交事务
commit;/commit work;
注:上面的begin/commit为事务的开始和结束,因为在前一步我们关闭了mysql的autocommit,所以需要手动控制事务的提交,在这里就不细表了。
上面的第一步我们执行了一次查询操作:select status from t_goods where id=1 for update;
与普通查询不一样的是,我们使用了select…for update的方式,这样就通过数据库实现了悲观锁。此时在t_goods表中,id为1的 那条数据就被我们锁定了,其它的事务必须等本次事务提交之后才能执行。这样我们可以保证当前的数据不会被其它事务修改。
注:需要注意的是,在事务中,只有SELECT ... FOR UPDATE 或LOCK IN SHARE MODE 同一笔数据时会等待其它事务结束后才执行,一般SELECT ... 则不受此影响。拿上面的实例来说,当我执行select status from t_goods where id=1 for update;后。我在另外的事务中如果再次执行select status from t_goods where id=1 for update;则第二个事务会一直等待第一个事务的提交,此时第二个查询处于阻塞的状态,但是如果我是在第二个事务中执行select status from t_goods where id=1;则能正常查询出数据,不会受第一个事务的影响。
补充:MySQL select…for update的Row Lock与Table Lock
上面我们提到,使用select…for update会把数据给锁住,不过我们需要注意一些锁的级别,MySQL InnoDB默认Row-Level Lock,所以只有「明确」地指定主键,MySQL 才会执行Row lock (只锁住被选取的数据) ,否则MySQL 将会执行Table Lock (将整个数据表单给锁住)。
举例说明:
数据库表t_goods,包括id,status,name三个字段,id为主键,数据库中记录如下;
- mysql> select * from t_goods;
- +----+--------+------+
- | id | status | name |
- +----+--------+------+
- | 1 | 1 | 道具 |
- | 2 | 1 | 装备 |
- +----+--------+------+
- 2 rows in set
- mysql>
注:为了测试数据库锁,我使用两个console来模拟不同的事务操作,分别用console1、console2来表示。
例1: (明确指定主键,并且有此数据,row lock)
console1:查询出结果,但是把该条数据锁定了
- mysql> select * from t_goods where id=1 for update;
- +----+--------+------+
- | id | status | name |
- +----+--------+------+
- | 1 | 1 | 道具 |
- +----+--------+------+
- 1 row in set
- mysql>
console2:查询被阻塞
console2:如果console1长时间未提交,则会报错
- mysql> select * from t_goods where id=1 for update;
- ERROR 1205 : Lock wait timeout exceeded; try restarting transaction
例2: (明确指定主键,若查无此数据,无lock)
console1:查询结果为空
console2:查询结果为空,查询无阻塞,说明console1没有对数据执行锁定
例3: (无主键,table lock)
console1:查询name=道具 的数据,查询正常
- mysql> select * from t_goods where name='道具' for update;
- +----+--------+------+
- | id | status | name |
- +----+--------+------+
- | 1 | 1 | 道具 |
- +----+--------+------+
- 1 row in set
- mysql>
console2:查询name=装备 的数据,查询阻塞,说明console1把表给锁住了
console2:若console1长时间未提交,则查询返回为空
例4: (主键不明确,table lock)
console1:查询正常
- mysql> begin;
- Query OK, 0 rows affected
- mysql> select * from t_goods where id>0 for update;
- +----+--------+------+
- | id | status | name |
- +----+--------+------+
- | 1 | 1 | 道具 |
- | 2 | 1 | 装备 |
- +----+--------+------+
- 2 rows in set
- mysql>
console2:查询被阻塞,说明console1把表给锁住了
例5: (主键不明确,table lock)
console1:
- mysql> begin;
- Query OK, 0 rows affected
- mysql> select * from t_goods where id<>1 for update;
- +----+--------+------+
- | id | status | name |
- +----+--------+------+
- | 2 | 1 | 装备 |
- +----+--------+------+
- 1 row in set
- mysql>
console2:查询被阻塞,说明console1把表给锁住了
console1:提交事务
console2:console1事务提交后,console2查询结果正常
- mysql> select * from t_goods where id<>2 for update;
- +----+--------+------+
- | id | status | name |
- +----+--------+------+
- | 1 | 1 | 道具 |
- +----+--------+------+
- 1 row in set
- mysql>
以上就是关于数据库主键对MySQL锁级别的影响实例,需要注意的是,除了主键外,使用索引也会影响数据库的锁定级别
举例:
我们修改t_goods表,给status字段创建一个索引
修改id为2的数据的status为2,此时表中数据为:
- mysql> select * from t_goods;
- +----+--------+------+
- | id | status | name |
- +----+--------+------+
- | 1 | 1 | 道具 |
- | 2 | 2 | 装备 |
- +----+--------+------+
- 2 rows in set
- mysql>
例6: (明确指定索引,并且有此数据,row lock)
console1:
- mysql> select * from t_goods where status=1 for update;
- +----+--------+------+
- | id | status | name |
- +----+--------+------+
- | 1 | 1 | 道具 |
- +----+--------+------+
- 1 row in set
- mysql>
console2:查询status=1的数据时阻塞,超时后返回为空,说明数据被console1锁定了
console2:查询status=2的数据,能正常查询,说明console1只锁住了行,未锁表
- mysql> select * from t_goods where status=2 for update;
- +----+--------+------+
- | id | status | name |
- +----+--------+------+
- | 2 | 2 | 装备 |
- +----+--------+------+
- 1 row in set
- mysql>
例7: (明确指定索引,若查无此数据,无lock)
console1:查询status=3的数据,返回空数据
console2:查询status=3的数据,返回空数据