Lock: What are pessimistic locking and optimistic locking

Foreword

Index and locks are two core knowledge database, whether at work or in the interview, we will deal with them often.
In the understanding of the four principles ACID transactions and different isolation levels, we know these isolation levels are achieved by a lock to complete, then you have not thought about why we need to lock it to the data?

The purpose of locking

Locking is to ensure data consistency, this idea is also important in the development of the field program. There will still be a multi-thread synchronization problems in program development. When multiple threads concurrently access a data, especially for some sensitive data (such as order, amount, etc.), we need to ensure that this data can take up only one thread at any time during the visit, and ensure data integrity consistency.

Divided according to the lock granularity

Lock for locking the data, divide lock the lock object from the particle size, respectively, row locks, page and table locks.

1, row locks: lock in accordance with the size of the data line. Lock intensity, low probability of conflict locks, high concurrency can be achieved, but the overhead is relatively large for the lock, the lock will be relatively slow, prone to deadlock.
2, page locks: lock on the page size, the data resource to be locked rows than locks, since a page can have a plurality of rows. When using the page lock, data appears the phenomenon of waste, but such a waste that is up to the data lines on a page. Page locks overhead between table and row locks, there will be a deadlock. Particle size is between the locking and row locks the table, the general concurrency.
3, table lock: the data table lock, locking large size, the probability of simultaneous lock conflict will be high, low concurrency data access. Benefit is the use of lock overhead is small, the lock will be soon.

Different databases and different storage engine supports the lock granularity. InnDB and Oracle support row and table locks. The only support MyISAM table locks, MySQL in the BDB storage engine supports page locks and table locks. SQL server can support row locks, page locks and table locks at the same time.
As shown below:
Here Insert Picture Description
each level of the data lock is limited, because the lock will occupy memory space, the size of the lock space is limited. When a certain number of locks exceeds the threshold level of this level, it will lock escalation. Lock escalation is to use greater efforts to lock replacement lock multiple smaller efforts, such as the Bank of China InnDB lock escalation to a table lock, the benefits of doing so is to reduce the space occupied by the lock, but at the same degree of concurrency data also declined.

Lock divide from the perspective of database management

Lock divide from the perspective of database management, is divided into:

(1) a shared lock: Also called a read lock or S locks. Shared locks resources can be read by other users, but can not be modified. During the select time, the object will be shared locks, when the data has been read, it will release the shared lock, so as to ensure that data is not modified during the reading.

(2) Exclusive lock: also called exclusive lock, write lock or X lock. Exclusive lock lock data only allows transactions using locking operation, other transactions can not be queried or modifications to the locked data.

Note: Add the exclusive lock table, only to get an exclusive lock transactions can query or modify tables, other things if you want to query the data in the table, we need to wait.

Intent locks : that is to a greater level schematic of the space inside has been on whether the lock.
If we add the data to a row exclusive lock, the database will automatically give a larger space, such as data pages, or data table plus intent locks, tell other people this data page or data sheets had already been on exclusive lock, so when other people want to get the data table exclusive lock, just need to know if someone has the intention to obtain the exclusive lock on the data table can be.

If the transaction you want to acquire a shared lock some records in the data table, you need to add a shared intent lock on the table.
If you want to get the exclusive lock transaction data table some records, you need to add on a data sheet intent exclusive lock.
At this time, intent lock will tell other transactions has already been locked up some of the records in the table can not be a full table scan of the entire table.

Lock divided from a programmer's perspective

From the programmer's perspective to see if the lock, the lock can be divided into optimistic and pessimistic locking, the name can be seen from the way of thinking of these two locks look at data concurrency.

(1) optimistic lock: think of concurrent operations on the same data does not always happen, is a small probability event, do not always lock the data, that is, without using the database itself lock mechanism, but implemented by a program. On the program, we can use the version number of the time stamp mechanism or mechanisms to achieve.

Optimistic locking programmers rights to control their own data concurrent operations, basically by adding a line to the data stamp (time stamp or version number), thus proving whether the current to get the latest data.

(2) pessimistic lock: is an idea, the data is held by a conservative approach to modify other matters, will be implemented by the database itself lock mechanism, so as to ensure exclusive data manipulation.
Here Insert Picture Description

Optimistic and pessimistic locking of application scenarios

1, more optimistic locking for read operation scene, relatively speaking, writing operation is relatively small. Its advantage is that the program implementation, the deadlock is not a problem, but will be relatively optimistic scenario applies, except that it can not prevent the program's database operations.
2, pessimistic locking for writing many operating scenarios, because the write operation exclusive. Using pessimistic locking way, you can prevent other transactions authority to operate the data in the database level, to prevent read - write and write - write conflicts.

Note: optimistic and pessimistic locking is not a lock, but the lock design.

The lock is present, a deadlock may occur.
Deadlock : is the phenomenon of multiple transactions (that is, if more than one process in the program level) in the implementation process, because a same resource competition caused by obstruction. A deadlock occurs, often because in a transaction, the lock is gradually acquiring a. In other words, when multiple transactions to obtain read locks for the same data may deadlock situation may occur.
Resolve a deadlock : When a deadlock occurs, you need to carry out a transaction rollback, another transaction to complete the transaction to acquire the lock, and then release the lock off, much like a traffic jam when solutions.

Some of the ways to avoid deadlock occurred:

1, if the transaction involves more than one table, the operation is more complicated, you can try to lock all the resources once, rather than gradually to acquire, thus reducing the probability of occurrence of deadlock.
2, if the transaction needs to be updated most of the data in the data table, data sheets and relatively large, then lock escalation methods can be used, such as row-level locks will be upgraded into a table-level lock, thereby reducing the probability of deadlock.
3, a plurality of different transactions concurrent read and write the data table, the table may agree sequential access, reducing the probability of the same order of deadlock.

to sum up

Multiple transactions of a competition the same resources will cause obstruction deadlock in the implementation process, so we need to find a way to avoid a deadlock occurs. However, in the database, there are some circumstances is not a deadlock occurs, such as the use optimistic locking manner. There is also not a deadlock in the MySQL MyISAM storage engine, because MyISAM is always time access to all the locks, so you can meet either all executed or need to wait for all.

In this article refer to the content of the curriculum geeks are welcome to share in the comments section to learn together ~

He published 189 original articles · won praise 212 · views 90000 +

Guess you like

Origin blog.csdn.net/Sophia_0331/article/details/105343597