mysql下事务和锁

 

 

 

事务一

事务二

 

start transaction;

 

 

 

start transaction;

P1

select c from a where a=1; 

 

 

 

update a set c=c+1 where a=1;

P2

select c from a where a=1 for update;

 

Q1

 

select c from a where a=1; 

 

 

commit;

P3

select c from a where a=1;

 

 

commit;

 

 

同一个事务中

select c from a where a=1 for update

select c from a where a=1

取出的值不一样

 

 

REPEATABLE READ (可重复读)

P1=P3=0

P2=Q1=1

READ COMMITTED

P1=0

P2=P3=Q1=1

 

 

 

 

事务一

事务二

 

start transaction;

 

 

 

 

P1

select c from a where a=1; 

 

 

 

start transaction;

 

 

update a set c=c+1 where a=1;

Q1

 

select c from a where a=1; 

P2

select c from a where a=1; 

 

 

 

commit;

P3

select c from a where a=1;

 

 

 

 

 

 一致性的非锁定读:

通过Undo段实现,不同的隔离级别读取的快照数据不同

多版本并发控制(Multi Version Concurrency Control, MVCC)

 

REPEATABLE READ (可重复读)

P1=P2=P3=0

Q1=1

READ COMMITTED

P1=P2=0

P3=Q1=1

 

 

 

事务一

事务二

结果(更新后

读最新值)

 

start transaction;

 

 

 

 

start transaction;

 

Q1

 

select c from a where a=1; 

0

 

 

update a set c=c+1 where a=1;

 

Q1

 

select c from a where a=1; 

1

 

 

commit;

 

P1

select c from a where a=1;

 

1

 

 

 

 

 

事务一

事务二

结果(更新后

读最新值)

 

start transaction;

 

 

 

 

start transaction;

 

P1

select c from a where a=1; 

 

0

 

 

update a set c=c+1 where a=1;

 

Q1

 

select c from a where a=1; 

1

 

update a set c=c+1 where a=1;

 

 

 

 

commit;

 

P2

select c from a where a=1;

 

3

 

 

 

 

 

 

 

事务一

事务二

 

begin;

 

P1

update a set a=2 where a=1;

 

 

 

begin;

Q1

 

update a set c=37 where a=1;

 

commit;

 

 

 

 写一致性

REPEATABLE READ (可重复读)

重新启动更新

READ COMMITTED

重新启动更新

 

事务一

事务二

next-key-lock

begin;

 

 

select c from a where a<3 lock in share mode;

 

 

 

begin;

 

 

Insert into a(a) select 1;

锁等待

Commit;

 

 

SELECT@@tx_isolation;

 

SET [SESSION |GLOBAL] TRANSACTION ISOLATION LEVEL {READ UNCOMMITTED | READ COMMITTED |REPEATABLE READ | SERIALIZABLE}

SET  SESSION TRANSACTION ISOLATION LEVELREPEATABLE READ;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值