database lock

Before talking about locks, understand transactions. The biggest feature of relational databases is transactions. A transaction guarantees that a sequence of operations performed by a single logical unit of work is either fully performed or not performed at all. The 4 important characteristics of transactions are as follows:

Atomic: A transaction must be an atomic unit of work; either all or none of its data modifications are performed. Often, operations associated with a transaction have a common goal and are interdependent. If the system performs only a subset of these operations, the overall goal of the transaction may be violated. Atomicity eliminates the possibility of the system processing subsets of operations.
Consistency: The consistency of a transaction means that the database must be in a consistent state before and after a transaction is executed. This property is called transactional consistency. A database is said to be consistent if its state satisfies all integrity constraints.
Isolation: Modifications made by concurrent transactions must be isolated from modifications made by any other concurrent transaction. The state of the data when the transaction views the data, whether it is the state before another transaction is executed or a state in the middle, and what influence there is between them, can be controlled by setting the isolation level.
Durability: After the transaction ends, the result of the transaction processing must be able to be solidified, that is, the data written to the database file will not be lost even if the machine is down, and its impact on the system is permanent.

Databases need a mechanism to ensure concurrency while still ensuring that operations meet transactional requirements. This mechanism is the lock. Locks and concurrency are interdependent and mutually restrictive. The larger the lock granularity, the smaller the concurrency. The smaller the granularity, the greater the concurrency, but at the same time, the more resources it needs to consume.

The following picture shows the knowledge about locks:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324865994&siteId=291194637
Recommended