Two types of locking problems in MySQL

Source: Internet
Author: User

MySQL describes two problems in 15.2.10.5, and gives the solution separately.
1. Write the data to the child table, but before writing, make sure that its corresponding information exists in the parent table.
It may appear that the data in the parent table has been read, but another request is deleted.
Way:

Begin;select info from father_table where name = ' Nike ' LOCK in SHARE mode;insert into son_table values (' someinfo '); commit ;

A transaction is a process that ensures atomicity of multiple processes, but does not guarantee the correctness of the above problems. In the transaction, the lock in SHARE MODE is used,
It sets a shared lock on the line we read so that the latest data can be read, and when you access a data that has the same shared lock,
Waits for the transaction to be committed before it is read. This avoids the problem that the data is modified when a transaction is not completed.

2. When a counter is implemented, the same field is modified, and when both users read and then modify it, a process may
Another process reads the data and writes the data again, so that a statistic is omitted.
Way:
1. Write data, write data lock, and read data is limited, only when the data added up, to read, and after the addition, it is the latest data.
2.

Begin;select count Form son_table for update;update son_table Set count = Count + 1;commit;

SELECT ... The for update reads the most recent visible data, setting an exclusive lock on each row it reads. Therefore, it sets the same lock as the lock that the search SQL update may set on the row.

The above example shows that when multiple users access the same row of data, a lock may occur, whereas a single user accessing only his own data is unlikely to lock up, for example, to modify a profile

Two types of locking problems in MySQL

Related Article

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.