<> Explain in detail what is pessimistic lock and optimistic lock ?

Pessimistic lock and optimistic lock must be asked during interview , It is also an important knowledge point in the development process , It's something a programmer has to master , Do you know what is a pessimistic lock and an optimistic lock ?

1. Pessimism and optimism :

Pessimistic view of the world as changing , Man is doomed to suffer , So I fell into pessimism and despair , Even think life is not like death , Think about everything in a bad way . Very negative .
Optimism generally refers to the universe , Sociology , An attitude of confidence and hope in life , Views and theories . In the history of ethics , Be optimistic about everything , A positive attitude
2. What about pessimistic lock and optimistic lock ?
Pessimism lock and optimism lock and pessimism are similar to optimism

Pessimistic lock : It's like having a pessimistic attitude , Every time I go to get the data, I think others will modify it , So every time I get the data, I lock it , So that if people want to take this data, they will block Until it gets the lock
The possibility of concurrency conflict in pessimistic lock assumption , So we can block all operations that may violate data integrity

Java synchronized Is one of the pessimistic locks , Every time a thread wants to modify data, it obtains the lock first , Ensure that only one thread can operate data at the same time , Other threads will be block.

Optimistic lock :
It's just optimistic , Every time I go to get the data, I think other people won't change it , So it's not locked , But in the process of updating, we will judge whether other people have updated this data during this period , You can use the method of adding field comparison , Such as version number and other mechanisms . Optimistic lock is suitable for the application type of reading more and writing less , This improves throughput .
3. Explain in detail what is version number mechanism :
Version number mechanism :
This is the most common way to implement optimistic locks , Add a data type of "version" To implement .

When reading data , take version The values of the fields are read out together , Every update of data ,version Add one to the value of . When you submit an update , Determine the corresponding version of database table version Value and the first version Value for comparison , If the values are the same , Update allowed , Otherwise, it is considered as overdue data .

Pessimistic lock format :
SELECT…FOR UPDATE
example :select * from db where name=“li” for update

This one sql Statement locked db All in the table meet the search criteria ( name=“li” ) Records of .
Before this transaction is committed ( Locks in the process of a transaction are released when the transaction is committed ), These records cannot be modified by the outside world .

format SELECT…FOR UPDATE NOWAIT

The meaning of this keyword is “ Don't wait , Return now ”, If the currently requested resource is locked by another session , Blocking will occur ,nowait This blockage can be avoided .

4. Use scenarios of two types of locks :
Optimistic lock : More reading and less writing
Pessimistic lock : Write more and read less

Technology
©2019-2020 Toolsou All rights reserved,
TP6 Application examples of verifier and correct verification data ESP8266/ESP32 System : Optimize system startup time 2021 year 2 Chinese programming language ranking 2021 year 1 Monthly programmer salary statistics , average 14915 element CSS architecture design It's not depravity that's terrible , It's about knowing you're falling Gude Haowen serial - You deserve to be an engineer ( Preface ) Software testing BUG describe C Course design of language programming of 《 Student achievement management system 》vue In the project axios Global encapsulation of