mysql for update 锁住了一条或多条数据,或者是整个表 如果事务一直没有提交(比如服务器挂掉了)会不会这表数据一直锁在那里
巴扎黑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. commit
或 rollback
。但是,如果客户端1直接关闭窗口断开连接,客户端2也能直接拿到锁。说明客户端断开时,会自动释放锁。
那么问题来了,客户端1断开连接后,是自动 rollback
rollback
? 🎜