The lock in innodb is released after the transaction is submitted/rolled back. Once the transaction is submitted/rolled back, the lock in the transaction will be automatically released. Innodb defaults to autocommit=1, which turns on automatic submission.
The difference between locks using indexes and not using indexes for retrieval conditions:
When the retrieval conditions have indexes, specific rows will be locked.
If the search conditions are not used, a full table scan will be performed to lock all rows (including non-existent records)
Read lock is Shared, or non-blocking. Multiple users can read the same resource at the same time without interfering with each other.
Write lock is exclusive, which means that a write lock will block other write locks and read locks. In addition, write locks have a higher priority than read locks, so a write lock request may be inserted in front of the read lock queue, but the read lock will not be inserted in front of the write lock.
InnoDB also has two table locks: intention shared lock (IS), intention exclusive lock (IX)
InnoDB implements two types of row-level locks, shared locks and exclusive locks
## Optimistic lock: Optimistic lock, also called optimistic concurrency control, assumes that multi-user concurrent transactions will not affect each other during processing, and each transaction can handle the part of its impact without generating a lock. data. Before committing data updates, each transaction will first check whether other transactions have modified the data after the transaction read the data. If there are updates from other transactions, the currently committing transaction will be rolled back. Pessimistic lock: Pessimistic lock, also called pessimistic concurrency control, when transaction A applies a lock to a certain row of data, and when this transaction releases the lock, other transactions can execute and The lock conflict operation, the lock imposed by transaction A here is called pessimistic lock. Shared locks and exclusive locks (row locks, gap locks, next-key locks) are both pessimistic locks How to implement pessimistic locks and optimistic locks: The implementation of pessimistic locks relies on the database The provided lock mechanism is implemented, such as select * from news where id=12 for update, while optimistic locking relies on recording the data version, that is, by adding the version number field to the table as a key factor in whether the submission can be successful. Shared lock (S): Shared lock is also called read lock. If a transaction acquires a shared lock of a data row, other transactions can acquire the shared lock. The shared lock corresponding to the row, but the exclusive lock cannot be obtained, that is, when a transaction reads a data row, other transactions can also read, but cannot add, delete or modify the data row Setting the shared lock: SELECT .... LOCK IN SHARE MODE; Exclusive lock (X): Exclusive lock is also called write lock. If a transaction acquires an exclusive lock on a data row, other transactions will No other locks (exclusive locks or shared locks) can be acquired on the row. That is, when a transaction reads a data row, other transactions cannot add, delete, modify or check the data row Setting an exclusive lock: SELECT .... FOR UPDATE
Note:
In MySQL, row-level locks do not directly lock records, but lock indexes.Indexes are divided into primary key indexes and non-primary key indexes. If a sql statement operates on the primary key index, MySQL will lock the primary key index; if a statement operates on a non-primary key index, MySQL will first lock the non-primary key index. , and then lock the relevant primary key index.
InnoDB row locks are implemented by locking index entries. If there is no index, InnoDB will lock records through a hidden clustered index. That is to say: if the data is not retrieved through index conditions, then InnoDB will lock all data in the table. The actual effect is the same as the table lock.
Record Lock: Lock the index item, that is, lock a record.
Gap Lock: Lock the 'gap' between index items, the gap before the first record or the gap after the last record, that is, lock a range of records, excluding the record itself
Next-key Lock: Lock a range of records and include the record itself (a combination of the above two)
Note:The default level of InnoDB is repeatable-read (repeated read)level. The ANSI/IOS SQL standard defines four transaction isolation levels: read uncommitted, read committed, repeatable read, serializable, Gap Lock and The difference between Next-key Lock:
Next-Key Lock is a combination of row lock and gap lock. In this way, when InnoDB scans the index record, it will first add row lock to the selected index record ( Record Lock), and then add gap lock (Gap Lock) to the gaps on both sides of the index record. If a gap is locked by transaction T1, other transactions cannot insert records in this gap.Row locks prevent other transactions from being modified or deleted, Gap locks prevent other transactions from being added, and the Next-Key lock formed by the combination of row locks and GAP locks jointly solves the problem of the RR sector. Phantom reading problem when writing data.
When to use table locks in InnoDB:
, butThe upper layer MySQL Server is responsible for. Only when autocommit=0, innodb_table_lock=1 (default setting), the InnoDB layer can know the table lock added by MySQL, and MySQL Server can sense the row lock added by InnoDB. In this case Only under this condition can InnoDB automatically identify deadlocks involving table-level locks; otherwise, InnoDB will not be able to automatically detect and handle such deadlocks.(2) When using LOCAK TABLES to lock InnoDB, be careful to set AUTOCOMMIT to 0, otherwise MySQL will not lock the table; before the end of the transaction, do not use UNLOCAK TABLES to release the table lock, because UNLOCK TABLES will implicitly commit the transaction; COMMIT or ROLLBACK cannot release the table-level lock added with LOCAK TABLES. You must use UNLOCK TABLES to release the table lock. The correct method is as follows:
For example: If you need to write table t1 and Read from table t
SET AUTOCOMMIT=0; LOCAK TABLES t1 WRITE, t2 READ, ...;[do something with tables t1 and here];COMMIT; UNLOCK TABLES;
After a deadlock occurs, InnoDB can generally detect it and make one transaction release the lock and roll back, and another acquire the lock to complete the transaction. However, InnoDB cannot completely automatically detect deadlocks when external locks or locks are involved. This needs to be solved by setting the lock wait timeout parameter innodb_lock_wait_timeout. It should be noted that this parameter is not only used to solve the deadlock problem. When concurrent access is relatively high, if a large number of transactions are suspended because they cannot obtain the required locks immediately, it will occupy a large amount of computer resources and cause serious performance problems. problems and even bring down the database. We can avoid this situation by setting an appropriate lock wait timeout threshold.
If different programs will access multiple tables concurrently, try to agree to access the tables in the same order. , which can greatly reduce the chance of deadlock. If two sessions access the two tables in a different order, the chance of deadlock is very high! But if accesses are performed in the same order, deadlock may be avoided.
In the same transaction, try to lock all the resources required at once to reduce the probability of deadlock.
For business parts that are very prone to deadlocks, you can try to upgrade the locking granularity and use table-level locking to reduce the possibility of deadlocks.
When the program processes data in batches, if the data is sorted in advance to ensure that each thread processes records in a fixed order, the possibility of deadlock can also be greatly reduced.
Under the REPEATEABLE-READ isolation level, if two threads use SELECT...ROR UPDATE on the same condition record at the same time to add an exclusive lock, if there is no record that matches the condition , both threads will lock successfully. The program discovers that the record does not yet exist and attempts to insert a new record. If both threads do this, a deadlock will occur. In this case, changing the isolation level to READ COMMITTED can avoid the problem.
When the isolation level is READ COMMITED, if both threads execute SELECT...FOR UPDATE first, determine whether there are records that meet the conditions, and if not, insert the records. At this time, only one thread can insert successfully, and the other thread will wait for a lock. When the first thread submits, the second thread will make an error due to the primary key again, but although this thread makes an error, it will obtain an exclusive lock! At this time, if a third thread applies for an exclusive lock, a deadlock will also occur. In this case, you can directly perform the insertion operation and then catch the primary key duplicate exception, or when encountering the primary key duplicate error, always execute ROLLBACK to release the acquired exclusive lock
ps: If a deadlock occurs, you can use the SHOW INNODB STATUS command to determine the cause of the last deadlock and improvement measures.
Summary: For theInnoDBtable, there are mainly the following points
(1) InnoDB marketing is based on indexes. If data is accessed without an index, InnoDB uses table locks. (2) InnoDB gap lock mechanism, and the reasons why InnoDB uses gap locks. (3) Under different isolation levels, InnoDB’s locking mechanism and consistent read strategy are different. (4) MySQL recovery and replication also have a great impact on the InnoDB lock mechanism and consistent read strategy. (5) Lock conflicts and even deadlocks are difficult to completely avoid. After understanding the lock characteristics of InnoDB, users can reduce lock conflicts and deadlocks through design and SQL adjustments, including:The above is the detailed content of Introduction to innoDB lock in mysql. For more information, please follow other related articles on the PHP Chinese website!