🔒 global lock h1>
locks the entire database instance. Use the Flush tables with read lock (FTWRL)
to keep the entire database read-only, after which the database update statement (add, delete), data definition statement (build, modify table structure), and update class transaction commit statement will be blocked
locks the entire database, so an obvious use case for global locks would be to do a full library logical backup; There is another way to use set global readonly=true
to make the entire library read-only, with one big difference:
exception handling, if the client is abnormal to disconnect, MySQL will automatically release the global lock, so that the whole library back to the normal state; After the entire library is set to READOnly, the database will remain readOnly after the exception is disconnected, resulting in the database being unwritable for a long time.
🔒 table level lock h1>
MySQL has two table level locks: table lock and meta data lock (MDL)
table lock syntax lock tables... Read /write
, unlock unlock talbes
. It is automatically released when the client is disconnected. In addition to restricting the reading and writing of other threads, table locks also restrict the following operation objects of this thread. For example, thread A executes lock tables t1 read,t2 write
, while other threads write t1 and read t2 are blocked. Before tables
, A thread can only read t1 and write t2
metadata lock is automatically added when accessing a table to ensure that the table structure cannot be modified
when accessing data
🔒 row locks h1>
MySQL row lock is implemented in each storage engine, InnoDB on the support of row lock, MyISAM engine does not support row lock
row lock is the lock that locks A row of records in the table. When A is processing this row of data and B also wants to process this data, B can only continue
after A has finished processing
two-stage locking protocol
in InnoDB transactions, row locks are added when they are needed, but they are not immediately released when they are not needed, but wait until the current transaction is completed.
so if more than one row needs to be locked in a transaction, put the rows that are most likely to cause concurrency as far back as possible.
an 🌰 : p>
the ticketing business of a ticketing system is approximately:
-
customer A ticket, A balance to deduct price p> li>
-
cinema B account balance increase ticket p> li>
-
record transaction log p> li> ol>
, the point of concurrency is cinema B to increase revenue. If each customer purchases tickets in the business order of 1, 2, and 3, then concurrent point 2 will hold the lock for a long time. If you change it to 3, 1, or 2 after processing the concurrent points, the row-level lock is released, reducing the wait time for locks between things.
deadlock and deadlock detection
different threads hold each other’s locks, A and B release the lock, B and A release the lock, then deadlock
deadlock treatment strategy:
-
one strategy is to go straight into the wait until the timeout occurs. This timeout can be set with the parameter innodb_lock_wait_timeout to
-
to initiate deadlock detection. When a deadlock is found, it will actively roll back a transaction in the death chain bar to allow other transactions to continue execution. The parameter innodb_deadlock_detect is set to on, indicating that this logic is turned on,
. Note here:
(1) deadlock detection is performed only when there is a row lock on the accessed row
(2) not all transactions are scanned for each deadlock detection. For example, at some point, the transaction waiting state is like this:
, B is waiting for A, D is waiting for C, and now there is an E, and it is found that E needs to wait for D, then E will judge whether there will be deadlock with D and C, this detection does not care about B and A
-
Read More:
- Unity Cursor Lock& Camera Lock
- When linux installs rpm, it prompts: can’t create transaction lock on /var/lib/rpm/.rpm.lock error
- DB2 encountered the problem of sqlcode = 911 lock table when updating record update
- MySQL error 1205 (HY000): lock wait timeout exceeded; try restarting transaction
- Centos 7 | mariadb/mysql | [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
- Lock mechanism in Oracle
- Lock wait timeout exceeded — transaction and index
- [338] MySQL error: error 1205: lock wait timeout exceeded solution
- Implementation of multithread sequential alternate execution by using lock in Java
- Java uses lock to realize multithread sequential alternate execution mode 2
- InnoDB: Unable to lock ibdata1, error: 35
- Waiting for another pilot command to release the startup lock
- mysqldump: Got error: 1016: Can’t open file: ‘./xxx.frm’ (errno: 24) when using LOCK TABLES
- Ansible Failed – apt lock
- Fatal: unable to create ‘project_ path/.git/ index.lock ‘: File exists.
- Forced shutdown of VMware wrokstation error (failed to get exclusive lock on the configuration file —)
- could not be opened to write the process output: fopen(/tmp\sf_proc_00.out.lock)
- VMware failed to lock memory file. Error restoring virtual machine state from file
- Permission denied error: unable to index file .vs/Trip2015/v15/Server/sqlite3/db.lock fatal: adding
- MySQL error code 1217 (ER_ROW_IS_REFERENCED): Cannot delete or update a parent row: a foreign key co