zoukankan      html  css  js  c++  java
  • 显式锁select for update 用法

    两个事务操作:

    set autocommit=off;

    A:

    begin;

    select * from students where id=1 for update;

    B:

    begin;

    select * from students where id=1;

    显示结果(直接查询,无需获得锁)

    select * from students;  

    显示结果

    select * from students where id=2 for update ;

    显示结果

    select * from students where id=1 for update;

    等待(事务A提交后才会获得该行行锁)


    案例:

    转账操作中需要判断余额是否足够

    begin;

    select balance from account where id=1;  ①

    (对余额进行判断,余额不做的不更新操作)

    update account set balance=balance-100 where id=1; ②

    update account set balance=balance+100 where id=2; 

    commit;

    多线程下,会有多个事务并发访问数据库。

    假设余额150,事务a可以执行到①判断余额充足,切换线程执行事务b到①并判断余额充足,最后都提交后,id为1的账户余额为-50;所以必须让整个事务操作保持原子性。

    修改①为:select balance from account where id=1 for update;对该记录加行锁。当事务a执行完提交释放行锁时事务b才能获得行锁 再查询判断。

  • 相关阅读:
    无题
    【HNOI 2002 】营业额统计
    P1589
    【网络流24题】最长递增子序列
    【NOI2008】志愿者招募
    【NOI2015】软件包管理器
    P1347
    【BZOJ 3262三维偏序】陌上花开
    数论六·模线性方程组
    数论五·欧拉函数
  • 原文地址:https://www.cnblogs.com/mryangbo/p/10805121.html
Copyright © 2011-2022 走看看