数据库事务的操作

数据库事务的操作

1.1 查看事务隔离级别

SHOW VARIABLES LIKE 'tx_isolation';

查看全局的事务隔离级别

SHOW GLOBAL VARIABLES LIKE 'tx_isolation';

使用系统变量查询

SELECT @@global.tx_isolation; SELECT @@session.tx_isolation; SELECT @@tx_isolation;

1.2 设置Mysql的事务隔离级别

# GLOBAL:设置全局的事务隔离级别 SESSION:设置当前session的事务隔离级别,如果语句没有指定GLOBAL或SESSION,默认值为SESSION
SET [GLOBAL | SESSION] TRANSACTION ISOLATION LEVEL
  {
       REPEATABLE READ
     | READ COMMITTED
     | READ UNCOMMITTED
     | SERIALIZABLE
   }

使用系统变量设置事务隔离级别

SET GLOBAL tx_isolation='REPEATABLE-READ'; SET SESSION tx_isolation='SERIALIZABLE';

2 案例分析

作为演示:product表

带着上面的我们来看一下,事务在没有隔离性的情况下,会引发哪些问题?

同时打开两个窗口模拟2个用户并发访问数据库(注:此处需要使用InnoDB引擎)

2.1 事务隔离级别设置为read uncommitted

	查询事务隔离级别 

SELECT @@tx_isolation;

	设置隔离级别为读未提交

SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

注意:需要同时修改两个窗口的事务隔离级别

时间轴 事务A 事务B
T1 start transaction;
T2 select p.productName,p.productCount from product p where p.productId=1;(productCount =100);
T3 start transaction;
T4 select p.productName,p.productCount from product p where p.productId=1;(productCount =100);
T5 update product set productCount = 99 where productId = 1;
T6 select p.productName,p.productCount from product p where p.productId=1;(productCount =99);
T7 ROLLBACK;
T8 select p.productName,p.productCount from product p where p.productId=1;(productCount =100);

T1—— A用户开启事务,start transaction; T2—— A用户查询当前小米手机剩余数量,select p.productName,p.productCount from product p where p.productId=1;此时数量显示为100。 T3——B用户开启事务,start transaction; T4——B用户查询当前小米手机剩余数量,select p.productName,p.productCount from product p where p.productId=1;此时数量显示为100。 T5—— B用户购买了一台小米手机,update product set productCount = 99 where productId = 1; 此时只修改数据并未提交事务。 T6—— A用户刷新页面,select p.productName,p.productCount from product p where p.productId=1;此时数量显示为99。 T7—— B用户购买失败,回滚事务。 T8—— A用户查询当前小米手机剩余数量,select p.productName,p.productCount from product p where p.productId=1;此时数量显示为100。

小结:

事务A读取了未提交的数据,事务B的回滚,导致了事务A的数据不一致,导致了事务A的脏读!

要避免这种问题的出现,那么就引出了读已提交。

2.2 事务隔离级别设置为read committed

	查询事务隔离级别 

SELECT @@tx_isolation;

	设置隔离级别为读未提交

SET SESSION  TRANSACTION ISOLATION LEVEL READ COMMITTED;

注意:需要同时修改两个窗口的事务隔离级别

时间轴 事务A 事务B
T1 start transaction;
T2 select p.productName,p.productCount from product p where p.productId=1;(productCount =100);
T3 start transaction;
T4 select p.productName,p.productCount from product p where p.productId=1;(productCount =100) ;
T5 update product set productCount = 99 where productId = 1 ;
T6 select p.productName,p.productCount from product p where p.productId=1;(productCount =100) ;
T7 commit;
T8 select p.productName,p.productCount from product p where p.productId=1;(productCount =99) ;

T1—— A用户开启事务,start transaction; T2—— A用户查询当前小米手机剩余数量,select p.productName,p.productCount from product p where p.productId=1;此时数量显示为100。 T3——B用户开启事务,start transaction; T4——B用户查询当前小米手机剩余数量,select p.productName,p.productCount from product p where p.productId=1;此时数量显示为100。 T5—— B用户购买了一台小米手机,update product set productCount = 99 where productId = 1; 此时只修改数据并未提交事务。 T6—— A用户刷新页面,select p.productName,p.productCount from product p where p.productId=1;此时数量显示为100。 T7—— B用户提交事务,购买成功。 T8—— A用户查询当前小米手机剩余数量,select p.productName,p.productCount from product p where p.productId=1;此时数量显示为99。

小结:

可以看到避免了脏读现象,但是却出现了,一个事务还没有结束,就发生了不可重复读问题(也就是说,这种隔离界别只能读到其他事务已经提交的事务),即事务A来说 productCount从 100->100->99。但这个过程中事务并未提交结束。

2.3 事务隔离级别设置为Repeatable Read(mysql默认级别)

	查询事务隔离级别 

SELECT @@tx_isolation;

	设置隔离级别为读未提交

SET SESSION  TRANSACTION ISOLATION LEVEL REPEATABLE READ;

注意:需要同时修改两个窗口的事务隔离级别

时间轴 事务A 事务B
T1 start transaction;
T2 select p.productName,p.productCount from product p where p.productId=1;(productCount =100);
T3 start transaction;
T4 select p.productName,p.productCount from product p where p.productId=1;(productCount =100) ;
T5 update product set productCount = 99 where productId = 1 ;
T6 select p.productName,p.productCount from product p where p.productId=1;(productCount =100) ;
T7 commit;
T8 select p.productName,p.productCount from product p where p.productId=1;(productCount =100);

T1—— A用户开启事务,start transaction; T2—— A用户查询当前小米手机剩余数量,select p.productName,p.productCount from product p where p.productId=1;此时数量显示为100。 T3——B用户开启事务,start transaction; T4——B用户查询当前小米手机剩余数量,select p.productName,p.productCount from product p where p.productId=1;此时数量显示为100。 T5—— B用户购买了一台小米手机,update product set productCount = 99 where productId = 1; 此时只修改数据并未提交事务。 T6—— A用户刷新页面,select p.productName,p.productCount from product p where p.productId=1;此时数量显示为100。 T7—— B用户提交事务,购买成功。 T8—— A用户查询当前小米手机剩余数量,select p.productName,p.productCount from product p where p.productId=1;此时数量显示为100。

小结:

可以看到可重复读隔离级别避免了脏读,不可重复读的问题,但是出现了幻读现象。事务A查询到的小米数量等于100,但是事务B修改了数量为99,但是事务A读取到的值还是100。当事务A去减1等于99时,是错误的,此时应该是99-1=98才对。接下来我们再提高一个事务隔离级别。

2.4 事务隔离级别设置为Serializable

	查询事务隔离级别 

SELECT @@tx_isolation;

	设置隔离级别为读未提交

SET SESSION  TRANSACTION ISOLATION LEVEL REPEATABLE READ;

注意:需要同时修改两个窗口的事务隔离级别

时间轴 事务A 事务B
T1 start transaction;
T2 start transaction;
T3 select p.productName,p.productCount from product p where p.productId=1;(productCount =100);
T4 update product set productCount = 99 where productId = 1;(等待中…)

T1—— A用户开启事务,start transaction; T2——B用户开启事务,start transaction; T3——A用户查询当前小米手机剩余数量,select p.productName,p.productCount from product p where p.productId=1;此时数量显示为100。T4——B用户要购买一部手机,这时候数据库就卡在了这里,然后过大概1分钟之后就会报错。

小结:

在我们Serializable隔离级别中,我们可以看到事务B去做修改动作时卡住了,不能向下执行。这是因为:给事务A的select操作上了锁,所以事务B去修改值的话,就会被卡住。只有当事务A操作执行完毕,才会执行事务B的操作。这样就避免了上述三个问题了。

总结一下:

不可重复读是由于数据修改引起的,幻读是由数据插入或者删除引起的。 

问题本身

  • 回到问题的本身,其实我们并不需要将事务提到这么高。

  • 问题的本身就是,当我们读完了的时候,就要在上面加锁。我们不希望别人能够去读它。因为别人读到了count,就会修改count的值,并写进去。所以我们在select 操作的时候,加上for update。这时候就会把这行操作给锁掉了。那么另外一个人也进行相同的操作,也表示select 出来的count需要进行update,需要锁住。

    select p.productName,p.productCount from product p where p.productId=1 for update;

PS: 在实际开发过程中,这样的加锁行为,是非常的耗系统性能的。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值