3

我遇到了一些表锁定问题。我在事务中锁定了一定数量的表。LOCK TABLES t1 READ, t2 READ, t3 READ, t4 READ,t5 READ,t6 READ,t7 READ;

虽然读取它没问题,但是当我尝试写入/更新此表时,它显示错误表 't1' is locked with a READ lock and can't be updated

但是我从来没有锁定表进行写入,我必须用更新的值写入这些表。

我的目标是我需要选择并且我需要更新行,而其他人不应该与上面的表交互,直到过程完成。

非常感谢您的解决方案!

4

2 回答 2

3

来自mysql 文档

锁获取规则 要在当前会话中获取表锁,请使用 LOCK TABLES 语句。可以使用以下锁类型:

读取 [本地] 锁:

持有锁的会话可以读取表(但不能写入)。

于 2015-01-09T14:38:03.177 回答
1
MariaDB [test]> lock table super1 read;
Query OK, 0 rows affected (0.00 sec)


MariaDB [test]> select * from super1;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  5 |
|  6 |
|  7 |
+----+
6 rows in set (0.00 sec)

MariaDB [test]> insert into super1 VALUE(10);
ERROR 1099 (HY000): Table 'super1' was locked with a READ lock and can't be updted

其他会话也可以选择

MariaDB [test]> unlock tables;
Query OK, 0 rows affected (0.00 sec)

MariaDB [test]> lock table super1 write;
Query OK, 0 rows affected (0.00 sec)

其他会话无法选择

MariaDB [test]> insert into super1 VALUE(10);
Query OK, 1 row affected (0.00 sec)

MariaDB [test]> insert into super1 VALUE(11);
Query OK, 1 row affected (0.00 sec)

MariaDB [test]> unlock tables;
Query OK, 0 rows affected (0.00 sec)
于 2016-03-16T09:04:28.687 回答