MySQL表鎖_LOCK TABLES_UNLOCK TABLES

MySQL表鎖_LOCK TABLES_UNLOCK TABLEShtml

mysql5.6mysql

參考文檔:http://dev.mysql.com/doc/refman/5.6/en/lock-tables.htmlsql

鎖表和解鎖表的基本語法

LOCK TABLES    
    tbl_name [[AS] alias] lock_type
    [, tbl_name [[AS] alias] lock_type] ...
    
UNLOCK TABLES
lock_type:
    READ [LOCAL]
  | [LOW_PRIORITY] WRITE


MySQL鎖表的目的

MySQL enables client sessions to acquire table locks explicitly(明白地,明確地) for the purpose of cooperating with other sessions for access to tables, or to prevent other sessions from modifying tables during periods when a session requires exclusive(單獨的,排外的) access to them. A session can acquire or release locks only for itself. One session cannot acquire locks for another session or release locks held by another session.數據庫


LOCK TABLES

LOCK TABLES explicitly acquires table locks for the current client session. Table locks can be acquired for base tables or views. You must have the LOCK TABLES privilege, and the SELECT privilege for each object to be locked.session

For view locking, LOCK TABLES adds all base tables used in the view to the set of tables to be locked and locks them automatically. If you lock a table explicitly with LOCK TABLES, any tables used in triggers are also locked implicitly.
app


UNLOCK TABLES

UNLOCK TABLES explicitly(明確的) releases any table locks held by the current session. LOCK TABLES implicitly(隱式的) releases any table locks held by the current session before acquiring new locks.oop

Another use for UNLOCK TABLES is to release the global read lock acquired with the FLUSH TABLES WITH READ LOCK statement, which enables you to lock all tables in all databases. ui


WRITE LOCK 和 READ LOCK的做用

A table lock only protects against inappropriate(不恰當的,不合適的) reads or writes by other sessions. A session holding a WRITE lock can perform table-level operations such as DROP TABLE or TRUNCATE TABLE. For sessions holding a READ lock, DROP TABLE and TRUNCATE TABLE operations are not permitted. TRUNCATE TABLE operations are not transaction-safe, so an error occurs if the session attempts one during an active transaction or while holding a READ lock.spa


示例一:

該示例下的會話的事務都是自動提交的.net

會話一:

mysql> lock tables people read;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from people;
+-----------+------------+-----------+
| person_id | first_name | last_name |
+-----------+------------+-----------+
|         1 | 1111       | 1111      |
|         2 | 2222       | 2222      |
+-----------+------------+-----------+
2 rows in set (0.00 sec)

mysql>

在這個會話中,鎖住表people,而後讀取數據,此時在另一個會話中,執行以下sql語句:

會話二

mysql> use local_database;
Database changed
mysql> lock tables people read;
Query OK, 0 rows affected (0.00 sec)

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from people;
+-----------+------------+-----------+
| person_id | first_name | last_name |
+-----------+------------+-----------+
|         1 | 1111       | 1111      |
|         2 | 2222       | 2222      |
+-----------+------------+-----------+
2 rows in set (0.00 sec)

mysql> lock tables people write;

當給表people上read lock時,正常執行,也能正常unlock table,可是當給people表上write lock 時,發現當前會話被阻塞。。。

此時在會話一種執行以下sql:

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

mysql>

好的,當執行完這句sql後,在會話二中的阻塞會消失。。

mysql> lock tables people write;
Query OK, 0 rows affected (50.40 sec)

mysql>

咱們能夠知道,一個會話中的read lock會阻塞另外一個會話中的write lock,但不會阻塞read lock。。。

咱們能夠這樣總結:

  • 表上的write lock會阻塞其餘會話中write lock 和 read lock

  • 表上的read lock只會阻塞其餘會話中write lock,而不會阻塞read lock

而行級鎖也是一樣的道理

我以前寫的這篇博客也是一樣的道理:http://my.oschina.net/xinxingegeya/blog/215417

繼續探索:

在會話二中給表加了write lock,在會話二進行以下操做:

mysql> lock tables people write;
Query OK, 0 rows affected (50.40 sec)

mysql> select * from people;
+-----------+------------+-----------+
| person_id | first_name | last_name |
+-----------+------------+-----------+
|         1 | 1111       | 1111      |
|         2 | 2222       | 2222      |
+-----------+------------+-----------+
2 rows in set (0.00 sec)

mysql> delete from people where person_id = 2;
Query OK, 1 row affected (0.15 sec)

這些都是沒問題的,刪除表的操做就不演示了。。。。

當給表加了read lock 時,進行以下操做:

mysql> lock tables people read;
Query OK, 0 rows affected (0.00 sec)

mysql> delete from people where person_id = 2;
ERROR 1099 (HY000): Table 'people' was locked with a READ lock and can't be updated
mysql>

能夠看到刪除操做不容許,雖然數據庫沒有id=2的記錄,但表被read lock了,因此不容許操做。。。

下面這些操做也不被容許:

mysql> drop table people;
ERROR 1099 (HY000): Table 'people' was locked with a READ lock and can't be updated
mysql> truncate people;
ERROR 1099 (HY000): Table 'people' was locked with a READ lock and can't be updated
mysql>

========END=========

相關文章
相關標籤/搜索