“This article has participated in the good Article call order campaign, click to view:Back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

preface

Books are never to be read but to be used

In this era of almost explosive learning materials, even you can easily access a few T videos, a small hand to collect a pile of articles, but rarely read. The process of learning is never just a simple look at it, for some practical technical books, if you really want to learn the knowledge, then it must be to use the book and absolutely not look. (PS: No grass grows when Lao Wang goes to war.)

Today the blogger will share optimism or pessimism, which lock do you choose? (optimistic lock/pessimistic lock), do not spray, if you have objections welcome to discuss!


The classification of the lock

  1. Fair lock/non-fair lock
  2. Reentrant lock
  3. Exclusive/shared locks
  4. Mutex/read/write lock
  5. Optimistic lock/pessimistic lock
  6. Segmented lock
  7. Biased lock/lightweight lock/heavyweight lock
  8. spinlocks

Optimistic Locking

The so-called optimism, in fact, is relative to the pessimistic lock, Baidu encyclopedia in the explanation.

Optimistic locking mechanism adopts a more loose locking mechanism. Pessimistic locking is implemented in most cases by relying on the database’s locking mechanism to ensure maximum exclusivity of operations. But with that comes a huge overhead on database performance, especially for long transactions, which is often unaffordable. Relatively pessimistic lock, optimistic lock is more prone to development and application. The above content is optimistically locked in baidu encyclopedia explanation, we need to find a scene to explain.

We take from the most classic case “Lao Wang withdraw money” for

1. In the figure, there are three existences, respectively representing Lao Wang, Lao Wang account, and the version information. Version information default is 1, at this time Lao Wang to buy something, the results found that money is not enough, then go to the bank to take some money to bai, decisive to the bank

He tells the teller to withdraw $5,000, and then the teller will deduct $5,000 from his balance, which is minus $5,000

At that time, I told the teller to withdraw money. The teller went back to read the card and found that the version information was 1.

Then at this time, Lao Wang, the teller planned to record the operation of -5000 into the database, and then change the version information to 2. At this time, the version information in the database is still 1

The money also got, the old wang happily took the money to go. Then, the teller on Daughter-in-law Wang’s side will have problems when operating. The version of the account information read out before is 1, but when he wants to operate, he finds that it is not right, someone has modified it, and this situation will occur.

But he wants to modify the somebody else is now the default is 2. This time he is 1 and 2 in comparison, and then submitted in time, at that time, the operation is completed, it’s too embarrassed, this kind of situation is proved that cannot let pharaoh daughter-in-law side of the cabinet, with a version of the data go to Lao wang’s side cover off. This is actually equivalent to an optimistic lock withdrawal.

Optimistic locking is mostly implemented based on the data Version recording mechanism. What is data version? Adding a version identity to the data is typically done by adding a “version” field to the database table in a versioning solution based on database tables. When the data is read out, the version number is read together, and when the data is updated, the version number is incremented by one. In this case, the version data of the submitted data is compared with the current version information recorded in the corresponding database table. If the version number of the submitted data is larger than the current version number of the database table, the data is updated; otherwise, the data is regarded as outdated.


Pessimistic locking

Baidu encyclopedia in the explanation

Pessimistic locks, as the name suggests, are strongly exclusive and exclusive. It refers to the conservative attitude of data being modified by the outside world (including other current transactions in the system, as well as transactions from external systems), thus keeping the data locked during the entire data processing process. The implementation of pessimistic locking often relies on the locking mechanism provided by the database (only the locking mechanism provided by the database layer can truly guarantee the exclusivity of data access, otherwise, even if the locking mechanism is implemented in this system, it cannot guarantee that the external system will not modify the data).

Because pessimistic locking assumes the worst case, every time you try to retrieve data, you think someone else will change it, so every time you try to retrieve the data, you lock it, and then you block it until it gets the lock.

The case comes: “The Salary thing”

This day Lao Wang opened the salary, the salary has been to the account, this time Lao Wang did not open SMS reminder, have to see the money to or not to?

Then I tell the teller, can you check my card?

When the teller is checking money, Lao Wang daughter-in-law came to take money again, the clothes bought last time are not good-looking, buy some more, money is not enough to come to the bank.

At this time, Lao Wang is looking up the money, and the pessimistic lock means that when I read, I am locked and you can’t see, so you can understand.

At this time, Lao Wang daughter-in-law is in a state of waiting, which is equivalent to pessimistic lock.

Because the pessimistic lock is that when we go to get the data, whether we intend to change it or not, the pessimistic lock assumes that we will change the data, so it will lock the data directly, and other people want to do the operation, you block until it is your turn to get the lock.


This is the difference between a pessimistic lock and an optimistic lock

  • Optimistic locking always thinks that there will be no concurrency problems. When fetching data, it always thinks that no other thread will modify the data, so it will not be locked. However, when updating the data, it will determine whether other threads have modified the data before this, generally using the version number mechanism or CAS operation.

  • Pessimistic locking always assumes the worst case scenario. Each time the data is fetched, it is assumed that another thread will change it, so the lock (read, write, row, etc.) is applied, and when other threads want to access the data, it needs to block and hang.

  • Like the little friend of the blogger can add a concern, point a like oh, continue to update hey hey!