码迷,mamicode.com
首页 > 其他好文 > 详细

lock tables和unlock tables

时间:2019-12-20 18:12:35      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:不能   ctrl   specified   另一个   rom   database   use   tab   bsp   

1、lock tables table1 read,table2 read,table3 read

igoodful@a8-apple-iphone-db00.wh(glc) > show tables;
+---------------+
| Tables_in_glc |
+---------------+
| mobile        |
| user          |
+---------------+
2 rows in set (0.00 sec)

Fri Dec 20 17:42:35 2019
igoodful@a8-apple-iphone-db00.wh(glc) > show open tables where in_use >=1;
+----------+-------+--------+-------------+
| Database | Table | In_use | Name_locked |
+----------+-------+--------+-------------+
| glc      | user  |      1 |           0 |
+----------+-------+--------+-------------+
1 row in set (0.00 sec)

Fri Dec 20 17:42:47 2019
igoodful@a8-apple-iphone-db00.wh(glc) > lock tables user read;                          ######### 添加读锁
Query OK, 0 rows affected (0.00 sec)

Fri Dec 20 17:43:03 2019
igoodful@a8-apple-iphone-db00.wh(glc) > show open tables where in_use >=1;              ########## 表明:添加表读锁后,表被用次数加一
+----------+-------+--------+-------------+
| Database | Table | In_use | Name_locked |
+----------+-------+--------+-------------+
| glc      | user  |      2 |           0 |
+----------+-------+--------+-------------+
1 row in set (0.00 sec)

Fri Dec 20 17:43:08 2019
igoodful@a8-apple-iphone-db00.wh(glc) > select * from user;                             ########## 表明:可以读取锁住的表的数据。
+------+------+
| id   | name |
+------+------+
|    1 | 1    |
|    2 | 1    |
+------+------+
2 rows in set (0.01 sec)

Fri Dec 20 17:43:37 2019
igoodful@a8-apple-iphone-db00.wh(glc) > select * from mobile;                            ##########  表明:只能读取锁住的表的数据,不能查看没有锁住的表的数据
ERROR 1100 (HY000): Table mobile was not locked with LOCK TABLES
Fri Dec 20 17:44:11 2019
igoodful@a8-apple-iphone-db00.wh(glc) >

 

 ########################################

在另一个会话线程中执行如下语句:

igoodful@a8-apple-iphone-db00.wh(glc) > select * from user;      ################# 表明:一个会话给表添加了读锁,那么不影响其他会话线程读取该表数据
+------+------+
| id   | name |
+------+------+
|    1 | 1    |
|    2 | 1    |
+------+------+
2 rows in set (0.00 sec)

Fri Dec 20 17:54:11 2019
igoodful@a8-apple-iphone-db00.wh(glc) > insert into user values (3,3);       ################# 表明:一个会话给表添加了读锁,其他会话线程只能对该表进行读取,而不能对该表执行执行dml和ddl语句。
^CCtrl-C -- sending "KILL QUERY 233531" to server ...
Ctrl-C -- query aborted.
ERROR 1317 (70100): Query execution was interrupted
Fri Dec 20 17:55:59 2019
igoodful@a8-apple-iphone-db00.wh(glc) > select connection_id();
+-----------------+
| connection_id() |
+-----------------+
|          233531 |
+-----------------+
1 row in set (0.00 sec)

Fri Dec 20 17:57:07 2019
igoodful@a8-apple-iphone-db00.wh(glc) > show open tables where in_use>0;;
+----------+-------+--------+-------------+
| Database | Table | In_use | Name_locked |
+----------+-------+--------+-------------+
| glc      | user  |      2 |           0 |
+----------+-------+--------+-------------+
1 row in set (0.00 sec)

ERROR:
No query specified

Fri Dec 20 17:57:25 2019
igoodful@a8-apple-iphone-db00.wh(glc) > lock tables user read;       ################# 表明:多个会话线程可以对同一张表添加读锁。
Query OK, 0 rows affected (0.00 sec)

Fri Dec 20 17:57:57 2019
igoodful@a8-apple-iphone-db00.wh(glc) >

 

 

当一个会话线程执行:

use glc;

lock tables user read;

那么

1)、该会话线程只能查询锁定的这几个表的数据,没有被锁定的表,不能查询到数据

2)、同时阻止其他会话对锁住的表进行事务操作语句和添加write表锁,

3)、其他会话可以对同一个表添加read锁,

4)

执行

lock tables和unlock tables

标签:不能   ctrl   specified   另一个   rom   database   use   tab   bsp   

原文地址:https://www.cnblogs.com/igoodful/p/12074396.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!