Transaction isolation level, optimistic locking, pessimistic locking

There is a transaction isolation level

read uncommited (read uncommitted): without any isolation, have dirty reads, unrepeatable right, phantom read problems

read committed (read submission): prevents dirty reads, does not prevent non-repeatable read and phantom read problems

repeated read (Repeatable Read): is possible to prevent dirty reads, non-repeatable read, can not be prevented (the default isolation level mysql) Magic Reading

serializable (serialization): the database is running as a serial, the above problems can be prevented, but low performance

The following explanations with the user table

name age
jason 38

Dirty read (a, b at the same time begin a transaction, a, b and then the transaction, which is not an end, after all, only two went into business, to end a transaction period, two transactions are alive)

1 a  开启事务  将jason的年龄改成 18岁,但是没有提交事务
2 b  开始事务  读取jason的年龄,发现是18岁。

上述的问题,
假设a事务回滚,b事务使用的数据就是错误的,就导致程序数据不正确。

Unrepeatable degree (a, b at the same time begin a transaction, a, b and then the transaction, which is not an end, after all, only two went into business, to end a transaction period, two transactions are alive)

1 a  开始事务  将jason的年龄改成18岁,但是提交了
2 b  开启事务  读取jason的年龄就能读取a事务修改后的jason年龄18岁。

Repeatable read (a, b at the same time begin a transaction, a, b and then the transaction, which is not an end, after all, only two went into business, to end a transaction period, two transactions are alive)

1 a  开始事务  将jason的年龄改成18岁,但是提交了
2 b  开启事务  读取jason的年龄不能读取a事务修改后的jason年龄18岁。而是读取的是38岁

Magic Reading (a, b at the same time begin a transaction, a, b and then the transaction, which is not an end, after all, only two went into business, to end a transaction period, two transactions are alive)

#概念和不可重读有点像,不可重复度是站在修改的基础上,而幻读是站在新增的基础上
1 a  开始事务  将所有的的年龄改成18岁
2 b  开始事务  新增一条数据数据name=tank,age=19,而且提交了。
3 a  重新查, 发现有一条数据 的age=19,这就是所谓的幻读。

Msql see how isolation levels

select @@global.tx_isolation;

How to modify the transaction isolation level

#修改配置文件
transaction-isolation=Read-Committed
#修改后一定要重启数据库

Optimistic and pessimistic locking lock database (the basis for the establishment of another transaction on)

Pessimistic locking

悲观锁的概念,他觉有人会修改我读的数据。那我就再查的时候,对数据进行锁定。

select age from user where id=1 for update;
比如 上面的查出来的 age =18 
update age改成age = age+ 1
如果该次查询走了索引,那就是行锁,如果没有走索引就是表锁。

如何释放锁呢?结束事务就释放了锁。
django中的orm是如果用悲观锁?
user.objects.select_for_update().filter(id=1).first()

Optimistic locking

乐观锁的本质不是锁。他是通过代码来实现锁的。他觉的别人不会修改的他的数据。他觉得读数据不会背别人修改。最后再修改该条数据,再where条件中添加,之前查出来的数据。以保证数据的安全性
select age from user where id=1 ;
比如 上面的查出来的 age =18   
20 
update age改成age = age+ 1  ====》19

乐观锁:
update age改成age = age+ 1 where age =18 and id =1  ====》19
如果我发现不是18,上面的影响行数是0

#如果是可重复读,上面的乐观锁无效,必须改成read committed
a update age改成age = age+ 1  ====》19 提交


b select age from user where id=1; --->18

update age改成age = age+ 1 where age =18 and id =1  ====》19  ---->数据已经被改

Guess you like

Origin www.cnblogs.com/yafeng666/p/12515266.html