V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
LeeReamond
V2EX  ›  问与答

Mysql 事务中使用排它锁无效可能是什么原因导致的?

  •  
  •   LeeReamond · Jan 29, 2022 · 1298 views
    This topic created in 1563 days ago, the information mentioned may be changed or developed.

    如题,开发中遇到一个问题

    usd test_db ;
    begin;
    select * from test_table where id = 2 for update;
    commit;
    

    上述代码如果多线程同时执行的话,在前面的线程执行完之前,后面的应该是阻塞的,然而换了一台机器到测试服务器上跑以后,该库中使用相同语句却所有线程都会同时执行完毕,也就是说排他锁失效了。

    想问一下这可能是什么原因导致的,以前没见过这种情况。我使用 explain 解释后确定搜索一定走的是索引(主键),所有搜索语句也都指定了条件,为什么会这样呢?

    3 replies    2022-01-31 03:01:40 +08:00
    sujin190
        1
    sujin190  
       Jan 29, 2022
    因为事务被 commit 了,事务 commit 了,你打开的排它锁自然也关闭了啊
    vzyw
        2
    vzyw  
       Jan 29, 2022
    id=2 的数据是不是空的? 如果为空,两个线程都能获得排他锁,不会阻塞
    LeeReamond
        3
    LeeReamond  
    OP
       Jan 31, 2022
    @sujin190 我为了表示这是一个事务加上了 begin 和 commit ,实际上测试锁的时候当然没有 commit

    @vzyw 非空,有数据
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5926 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 06:04 · PVG 14:04 · LAX 23:04 · JFK 02:04
    ♥ Do have faith in what you're doing.