<> 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 :
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