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

What are the ways to implement optimistic locks?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

What are the ways to implement optimistic locks? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Optimistic locking (Optimistic Locking) adopts a looser locking mechanism than pessimistic locking. Pessimistic locks are mostly realized by the locking mechanism of the database to ensure the maximum exclusivity of the operation. But it is followed by a large amount of database performance overhead, especially for long transactions, which is often unbearable. The optimistic locking mechanism solves this problem to some extent. Optimistic locks are mostly implemented based on data version (Version) recording mechanism.

1. Version number mechanism

Generally speaking, a database version number version field is added to the data table to express the number of times the data has been modified. When the data is modified, its version value will be increased by 1.

Such as:

Of course, when thread A needs to update the data value, it will also read the version value while reading the data. When submitting the update, if the version value just read is equal to the version value in the current database, otherwise, the update operation will be retried until the update is successful.

2. CAS algorithm

CAS (compare and swap) compares and swaps, with three operands, the memory address V, the expected value B, and the target child A to be replaced.

When the CAS instruction is executed, compare whether the memory address V is equal to the expected value B, if equal, assign A to B, (if not equal, cycle through the comparison until equal) the whole comparison assignment operation is an atomic operation.

Disadvantages of CAS:

(1) the cycle time overhead is large: when the memory address V is not equal to the expected value B, the cycle will be compared until it is equal.

(2) the atomic operation of only one shared variable can be guaranteed.

(3) if a variable V is read as A value for the first time, and it is checked that it is still A value when preparing for assignment, can we indicate that its value has not been modified by other threads? Obviously not, because during this time its value may be changed to another value, and then changed back to A, then the CAS operation will think that it has never been changed, and this problem is called the "ABA" problem of the CAS operation.

The answers to the questions about the implementation of optimistic locks are shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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