Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of realizing pessimistic Lock in mysql

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you the methods of realizing pessimistic lock in mysql. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Brief introduction:

Pessimistic lock, as its name suggests, has strong monopolistic and exclusive characteristics. It refers to a conservative attitude towards the modification of data by the outside world (including other transactions of the system, as well as transactions from external systems), so that the data is locked during the whole data processing process. The realization of pessimistic locking often depends on the locking mechanism provided by the database (only the locking mechanism provided by the database layer can really ensure the exclusivity of data access, otherwise, even if the locking mechanism is implemented in this system, there is no guarantee that the external system will not modify the data.

Mysql's pessimistic approach to locking:

1. Start a transaction

Begin;/begin work;/start transaction; (just choose one of the three)

2. Inquire out the commodity information

Select status from table where id=1 for update

3. Generate orders according to commodity information

Insert into table111 (id,goods_id) values (null,1)

4. Change the status of the product to 2

Update table set status=2 where id=1

5. Commit the transaction

Commit;/commit work; (any release lock)

Summary:

1. Realize pessimistic lock by using select. For update lock. Use commit to release the lock after the operation is completed.

2. In the innodb engine, the default row-level lock will lock a row when there is an explicit field, such as no query condition or condition

When the field is not clear, the whole table will be locked, and if the condition is range, the whole table will be locked.

3. When no data is found, the table will not be locked.

The above is the method of realizing pessimistic lock in mysql shared by Xiaobian. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report