In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to understand Java pessimistic lock and optimistic lock". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
one
Lock (Lock)
Before we introduce pessimistic and optimistic locks, let's take a look at what a lock is.
Locks can be seen everywhere in our lives. There are locks on our doors and safes where we save money, which are used to protect our property.
There are also locks in the program, and when multiple threads modify shared variables, we can syncronized the modification operation.
When multiple users modify the same data in the table, we can lock that data (row lock). Therefore, the lock actually controls the sequential execution of multiple operations under concurrency, so as to ensure the change of data security.
Moreover, locking is a mechanism and means to ensure data security, which is not specific to a particular technology. The same is true of pessimistic locks and optimistic locks. The pessimistic and optimistic locks introduced in this article are based on the database level.
two
Pessimistic lock
Pessimistic lock (Pessimistic Concurrency Control), the first sight of it, I believe everyone will think that this is a pessimistic lock. Yes, it is a pessimistic lock.
So where is this pessimism reflected? Pessimism is a negative emotion of our human beings. corresponding to the pessimistic mood of the lock, the pessimistic lock thinks that the data protected by it is extremely insecure and may change all the time. After a transaction gets the pessimistic lock (which can be understood as a user), no other transaction can modify the data and can only wait for the lock to be released.
The row locks, table locks, read locks, write locks, and syncronized implemented locks in the database are pessimistic.
Here again introduce what is the database table lock and row lock, lest some students do not understand the implementation of the pessimistic lock.
The database we often use is the most commonly used engine in mysql,mysql. Innodb,Innodb uses row locks by default. Row locks are index-based, so if you want to add a row lock, you must hit the index when you add the lock, otherwise a table lock will be used.
three
Optimistic lock
In contrast to pessimism, optimism is a positive emotion for us. The "optimism" of Optimistic Concurrency Control is that it believes that data will not change too often. Therefore, it allows multiple transactions to change the data at the same time.
However, optimism does not mean irresponsibility, so how to be responsible for multiple transaction sequences to modify the data?
Optimistic locking is usually achieved by adding a version (version) or timestamp (timestamp) to the table, of which versions are the most commonly used.
When a transaction fetches data from the database, it will also take out the version of the data (v1). When the transaction changes the data and wants to update it to the table, it will compare the previously extracted version v1 with the latest version v2 in the data. If v1=v2, it means that no other transaction modifies the data during the data change. At this time, the transaction is allowed to modify the data in the table. And when modified, the version will be added by 1 to indicate that the data has been changed.
If v1 is not equal to v2, it means that during the data change, the data has been changed by other transactions, and the data is not allowed to be updated to the table at this time. The general way to deal with it is to notify the user to re-operate. Unlike pessimistic locks, optimistic locks are artificially controlled.
four
How to realize
Through the above study, we know that pessimistic lock and optimistic lock are used to control the order change of data under concurrency. So let's simulate a scenario that needs to be locked to see what goes wrong without locking and how to solve it with pessimistic and optimistic locks.
Scene: users An and B both want to eat dried pork recently, so they open a shopping website and find the same store that sells dried pork. Below is the goods structure of the store's merchandise table and the data in the table.
It can be seen from the table that there is only one dried pork at present. In the case of unlocked, it is possible to oversell if AMagre B places an order at the same time.
Pessimistic lock solution
The solution to the pessimistic lock is that we think that the probability of conflict caused by data modification is relatively high, so before the update, we show to lock the record to be modified until we have modified it and then release the lock. During the locking period, only you can read and write, and other transactions can only read and not write.
An adds pessimistic lock (line lock) to the data of dried pork (id=1) before placing an order. At this time, this line of data can only be operated by A, that is, only A can buy it. B if you want to buy it, you must wait all the time.
After A has bought it, B will find that the quantity is already 0 when he wants to buy it again, so B will give up the purchase when he sees it.
So how to add pessimistic locks to pork jerky, that is, id=1? We can put a pessimistic lock on this line of data in id=1 with the following statement
Select num from goods where id = 1 for update; "how to understand Java pessimistic Lock and optimistic Lock" ends here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.