Interviewer: Will select...for update lock the table or lock the row?

 
  
 
  
您好,我是路人,更多优质文章见个人博客:http://itsoku.com

The select query statement will not be locked, but select.......for update will not only have the function of query, but also lock, and it is a pessimistic lock.

So whether it adds a row lock or a table lock depends on whether the index/primary key is used.

If no index/primary key is used, it is a table lock, otherwise it is a row lock.

verify:

Create table sql

//id为主键 
//name 为唯一索引
CREATE TABLE user (
 id INT ( 11 ) NOT NULL AUTO_INCREMENT,
 name VARCHAR ( 255 ) DEFAULT NULL,
 age INT ( 11 ) DEFAULT NULL,
    code VARCHAR ( 255 ) DEFAULT NULL,
 PRIMARY KEY ( id ),
    KEY idx_age ( age ) USING BTREE 
) ENGINE = INNODB AUTO_INCREMENT = 1570068 DEFAULT CHARSET = utf8

Automatic submission needs to be turned off, set @@autocommit=0; to manual submission. 0 means manual submission, 1 means automatic submission.

d3271f01824ee2f7548433ca2514becc.png

Combine with example verification

Example 1:

Use the primary key id as the condition to query, and then open another transaction to update the data. The update is blocked, locked, and the row data with the id of 1 to be queried is locked.

Figure 1 is the first transaction, and no transaction is committed

Figure 2 shows the second transaction, to update data, is blocked

Figure 3 shows the second transaction, where the lock was not obtained for a long time and an error was reported.

e6dd393540db79fa299ad6cbc0b7def3.png

9f10842beb8033ff018191e396083c5a.png

7f160cd531866a593d651c12c3849f72.png

Example 2:

We are starting a transaction to update another piece of data with id 2,

2a72008280549a5b8b300408b9a4ca39.png

fd2faf738a7e72a06f80bae014cc943b.png

Example 3 (index):

At the beginning of creating the table, a unique index was created on age.

ca56e90faaecccf84b76fbc999dc5a3f.png

98bebdd2e59a8be7b174f8af627bd833.png

3e6e0a7fd2a1a097e85fa2070fee6446.png

Example 4:

Use common field codes to operate

d027ee7a54856ffe8dfcb627f1951268.png

70ec1551d08646e1329acdbfcd342646.png

cfed32ef5dae191a21de857650cff6eb.png

In another transaction, I will update another piece of data. If the update is successful, it will lock the row, and if it fails, it will lock the table.

26d6a43e36bd0ff6a48f19681bd5dc14.png

0bf955086c642c90da6fadd046eebd5a.png

result:

If the query condition uses an index/primary key, then select ..... for update will lock the row.

If it is an ordinary field (no index/primary key), then select ..... for update will lock the table.

more good articles

  1. Java High Concurrency Series (34 articles in total)

  2. MySql master series (27 articles in total)

  3. Maven master series (10 articles in total)

  4. Mybatis series (12 articles in total)

  5. Talk about common implementations of db and cache consistency

  6. Interface idempotence is so important, what is it? How to achieve it?

  7. Generics, a bit difficult, will make many people confused, that's because you didn't read this article!

↓↓↓ 点击阅读原文,直达个人博客
你在看吗

Guess you like

Origin blog.csdn.net/likun557/article/details/132074125