Home > Database > Mysql Tutorial > There are several common ways to implement optimistic locking.

There are several common ways to implement optimistic locking.

王林
Release: 2020-07-03 11:49:44
Original
7995 people have browsed it

There are two common ways to implement optimistic locking, namely: 1. Version number mechanism; 2. CAS algorithm. Among them, implementing optimistic locking through the version number mechanism is the most classic method. The version number mechanism generally adds a database version number version field to the data table.

There are several common ways to implement optimistic locking.

Implementation method:

(Recommended learning: mysql tutorial)

1 , Version number mechanism

Generally means adding a database version number version field 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.

For example:

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 Update only when the version values ​​in the current database are equal, otherwise retry the update operation until the update is successful.

2. CAS algorithm

CAS (compare and swap) comparison and swap, there are three operands, memory address V, expected value B, the target to be replaced Sub A.

When the CAS instruction is executed, it compares whether the memory address V and the expected value B are equal. If they are equal, A is assigned to B. (If they are not equal, the comparison will be repeated until they are equal.) The entire comparison assignment operation is an atomic operation.

CAS Disadvantages:

(1) Large cycle time overhead: when the memory address V is not equal to the expected value B, it will continue to be compared in a loop until they are equal;

(2) Only the atomic operation of a shared variable can be guaranteed;

(3) If a variable V is the value A when it is first read, and when preparing to assign the value, it is checked that it is still the value A, then we can Does it mean that its value has not been modified by other threads? Obviously not, because during this period its value may be changed to other values, and then changed back to A. Then the CAS operation will think that it has never been changed. This problem is called the "ABA" of the CAS operation. " question;

The above is the detailed content of There are several common ways to implement optimistic locking.. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template