search

Home  >  Q&A  >  body text

mysql for update 如果事务一直没有提交会不会这表数据一直锁在那里

mysql for update 锁住了一条或多条数据,或者是整个表 如果事务一直没有提交(比如服务器挂掉了)会不会这表数据一直锁在那里

迷茫迷茫2781 days ago1235

reply all(1)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 16:29:28

    No. After the client connection is disconnected, the lock will be automatically released.

    Client 1

    set AUTOCOMMIT = 0;
    BEGIN;
    SELECT * FROM articles WHERE id=1 FOR UPDATE ;
    

    Client 2

    set AUTOCOMMIT = 0;
    BEGIN;
    SELECT * FROM articles WHERE id=1 FOR UPDATE ;
    

    At this time, client 2’s query will be stuck. Until client 1 commit or rollback . However, if client 1 directly closes the window and disconnects, client 2 can also directly obtain the lock. Explain that when the client disconnects, the lock will be automatically released. commitrollback 。但是,如果客户端1直接关闭窗口断开连接,客户端2也能直接拿到锁。说明客户端断开时,会自动释放锁

    那么问题来了,客户端1断开连接后,是自动 rollback

    Then the question is, after client 1 disconnects, does it automatically rollback? 🎜

    reply
    0
  • Cancelreply