This episode we talk about a "lock."
What is the lock |
Lock mechanism used to manage concurrent access to shared resources.
1, lock and latch difference:
lock and latch are that "lock", the difference is that the former lock is "transaction", which lock is "thread." We discussed herein is lock.
2, lock algorithm:
Row lock algorithm has three.
(1) Record Lock
locks on individual rows
(2) Gap Lock
gap lock, a lock range, but does not contain the records themselves.
(3) Next-key Lock
integrated two locks, a lock range, and comprising a lock record itself
Lock Category |
1, the basic lock
Basic lock is divided into two kinds, which is a row lock, so compatible or rejection for the same record for the
- Shared lock (s): shared lock is a "friendly" kids, happy to share
- Exclusive lock (x): exclusive lock is a "high-handed" children like to eat alone part, do not like to share
Thus, as long as there is x lock, you can not share data.
2, intent lock
Intention locks are table-level locking, which is also divided into two types:
- Intention shared lock
- Intent Exclusive lock
3, locking read
(1) consistent non-locking read
if the line is being read or update the delete operation, then the read operation will not be to wait for the release of the line locked, but a snapshot of the data to read rows. This is the default read mode.
(2) Read Lock Consistency:
displaying the read operation to lock the database to ensure consistency of the data logic.
4, self-locking growth
When we develop, often used growth since the id. In InnoDB, each table containing a value from the growth has a self-increment counter, when data inserted in, calculates the value from the counter into the growth from the growth column (e.g., id column).
Features:
This is a table lock, but it is not released until after a transaction is completed, but released immediately after the completion of self-growth values into sql statement. This is done to improve performance.
5, the outer catch
A foreign key reference for checking the integrity constraints. In InnoDB, for a foreign key column, if this is not explicitly cited Lie Jiasuo, InnoDB automatically add an index thereof.
Lock problems |
1, phantom read
In the same transaction, executed twice in a row the same SQL statement, may lead to different results, second sql statement may return line does not exist before.
2, dirty read
In a different transaction, current transaction can read data additional uncommitted transactions.
A read data transaction uncommitted transaction B, and on this basis and do other operations.
3, non-repeatable read
Transaction A Transaction B reads the change data has been submitted.
4, lost updates
Update operations of a transaction is covered by another transaction update operation, leading to inconsistent data.
other questions |
1, obstruction:
Because the compatibility relationships between different locks, at some time, a transaction locks need to wait for another transaction locks to release the resources occupied by him.
2, Deadlock:
Deadlock is a phenomenon of two or more transactions in the course of execution, a lock due to competition for resources caused by waiting for each other.
3, lock escalation:
Lock escalation refers to the current lock granularity decreased. InnoDB lock escalation is not a problem, since it is managed on a per-page for each transaction locks access, rather than on each record.
summary |