In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
What are pessimistic locks and optimistic locks? Many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can gain something.
Optimistic lock (Optimistic Locking)
Optimistic locking (Optimistic Locking) adopts a looser locking mechanism than pessimistic locking. Pessimistic locks are mostly realized by the locking mechanism of the database to ensure the maximum exclusivity of the operation. But it is followed by a large amount of database performance overhead, especially for long transactions, which is often unbearable. The optimistic locking mechanism solves this problem to some extent. Optimistic locks are mostly implemented based on data version (Version) recording mechanism. What is the data version? That is, to add a version identity to the data, which is generally achieved by adding a "version" field to the database table in the version solution based on the database table. When reading out the data, read out the version number together, and then add one to the version number when it is updated. At this point, the version data of the submitted data is compared with the current version information recorded in the corresponding database table, and if the submitted data version number is equal to the current version number of the database table, it is updated, otherwise it is considered to be out-of-date data.
The optimistic locking mechanism avoids the database locking overhead in long transactions (operator An and operator B do not lock the database data during the operation), and greatly improves the overall performance of the system under large concurrency.
Optimistic concurrency control believes that the probability of data contention between transactions (data race) is relatively small, so do it as directly as possible and do not lock it until commit time, so there are no locks or deadlocks.
Pessimistic lock (Pessimistic Lock)
Pessimistic lock, as its name suggests, has strong monopolistic and exclusive characteristics. It refers to a conservative attitude towards the modification of data by the outside world (including other transactions of the system, as well as transactions from external systems), so that the data is locked during the whole data processing process. The realization of pessimistic locking often depends on the locking mechanism provided by the database (only the locking mechanism provided by the database layer can really ensure the exclusivity of data access, otherwise, even if the locking mechanism is implemented in this system, there is no guarantee that the external system will not modify the data.
It is called pessimistic lock because it is a kind of concurrency control method that is pessimistic about data modification. We generally think that the probability of data being modified concurrently is relatively high, so we need to lock it before modifying it.
Pessimistic locks are mainly divided into shared locks or exclusive locks.
Shared lock [Shared lock] is also known as read lock, or S lock for short. As the name implies, a shared lock means that multiple transactions can share a lock for the same data, and all can access the data, but can only be read and not modified.
Exclusive lock [Exclusive lock] is also called write lock, or X lock for short. As the name implies, exclusive locks cannot coexist with other locks. If a transaction acquires an exclusive lock on a data row, other transactions can no longer acquire other locks on that row, including shared locks and exclusive locks, but transactions that acquire exclusive locks can read and modify data rows.
Pessimistic concurrency control is actually a conservative strategy of "lock first and then access", which ensures the security of data processing.
However, in terms of efficiency, the mechanism of handling locking will incur additional overhead on the database and increase the chances of deadlocks. Parallelism is also reduced so that if a transaction locks a row of data, other transactions must wait for the transaction to finish processing that row of data.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.