Home > Database > Mysql Tutorial > Definition of pessimistic lock and optimistic lock

Definition of pessimistic lock and optimistic lock

hzc
Release: 2020-07-02 16:07:19
Original
4925 people have browsed it

Pessimistic lock, as its name suggests, has strong exclusive and exclusive characteristics. It refers to a conservative attitude towards data being modified by the outside world. The optimistic locking mechanism adopts a more relaxed locking mechanism. Compared with pessimistic locking, optimistic locking is also a mechanism to avoid data processing errors caused by database phantom reading, long business processing time and other reasons.

Definition of pessimistic lock and optimistic lock

Optimistic Locking (Optimistic Locking)

Optimistic locking is relative to pessimistic locking. Optimistic locking assumes that data Under normal circumstances, conflicts will not occur, so when the data is submitted for update, the conflict of the data will be officially detected. If a conflict is found, an error message will be returned to the user to let the user decide what to do.

Baidu Encyclopedia:
The optimistic locking mechanism adopts a more relaxed locking mechanism. Optimistic locking is relative to pessimistic locking. It is also a mechanism to avoid data processing errors caused by phantom reads in the database and long business processing time. However, optimistic locking does not deliberately use the locking mechanism of the database itself, but relies on the data itself. to ensure the accuracy of the data.

Compared with pessimistic locking, optimistic locking does not use the locking mechanism provided by the database when processing the database. The general way to implement optimistic locking is to record the data version.

Definition of pessimistic lock and optimistic lock

Optimistic concurrency control believes that the probability of data race between transactions is relatively small, so proceed as directly as possible and do not lock until submission , so no locks or deadlocks will occur.

Pessimistic Lock

When you want to modify a piece of data in the database, in order to avoid being modified by others at the same time, the best way is Lock the data directly to prevent concurrency. This method of using the database lock mechanism to lock the data before modifying it and then modify it is called pessimistic concurrency control [also known as "pessimistic lock", Pessimistic Concurrency Control, abbreviation "PCC"].

Baidu Encyclopedia:
Pessimistic lock, as its name suggests, has strong exclusive and exclusive characteristics. It refers to a conservative attitude towards data being modified by the outside world (including other current transactions of the system and transaction processing from external systems). Therefore, the data is kept 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, there is no guarantee that the external system will not modify it. data).

The reason why it is called pessimistic locking is because it is a concurrency control method that has a pessimistic attitude towards data modification. We generally believe that the probability of data being modified concurrently is relatively high, so it needs to be locked before modification.

Pessimistic locks are mainly divided into shared locks or exclusive locks

  • Shared lock [Shared lock] is also called read lock, or S lock for short. As the name suggests, a shared lock means that multiple transactions can share a lock on the same data, and they can all access the data, but they can only read and cannot modify it.

  • Exclusive lock [Exclusive lock] is also called write lock, or X lock for short. As the name suggests, 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 the row, including shared locks and exclusive locks. However, transactions that acquire exclusive locks can Read and modify data rows.

Pessimistic concurrency control is actually a conservative strategy of "get the lock first and then access", which provides a guarantee for the security of data processing.

Definition of pessimistic lock and optimistic lock

But in terms of efficiency, the locking mechanism will cause additional overhead to the database and increase the chance of deadlock. In addition, parallelism will be reduced. If a transaction locks a row of data, other transactions must wait for the transaction to be processed before processing that row of data.

The above is the detailed content of Definition of pessimistic lock and optimistic lock. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template