A special deadlock in MySQL

Source: Internet
Author: User

Looking back at the CASE of online database (5.1.68), a large number of thread_running occurs during the peak period. It is found that the thread is in the deadlock State and only one table is involved, and there is only one row:

Table

('Id' smallint (5) unsigned not null default '0 ',

'Key' varchar (32) not null,

'Value' varchar (32) not null,

'Time' int (10) unsigned not nul) engine = innodb;

The database version is 5.1.68 innodb-plugin. The original logic is:

Lock table awrite; read and update the table; unlocktable;

Later, the logic begin; select id, key, value from a for update; commit;

In the case of a problem, these two logics exist at the same time, resulting in deadlocks at the MySQL Server layer and the Storage layer;

A sequence is as follows:

Session1:

Begin;

Select * from a for update; -- lock everyrow in execlusive mode


Session2:

Lock table a write; -- lock table a in server


Session2:

Update a set key = xxx where id = xxx -- holdserver lock and acquire row lock

Session1:

Same as above; -- hold row lock andacquire server lock

A deadlock occurs because only a part of the RD release code is released and rolled back and forth. Another Solution is to change the table to the MyISAM engine, which breaks the row lock;

However, this problem is solved by introducing MDL in 5.5. When session2 executes lock table t write, it will be blocked on: Waiting for table metadata lock, breaking the deadlock condition.

This article is from "MIKE's old blog" blog, please be sure to keep this source http://boylook.blog.51cto.com/7934327/1298750

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.