简体   繁体   English

锁定表以读取 MYSQL

[英]LOCK a table for READ MYSQL

I want to lock a particular table for reading but when I queried我想锁定一个特定的表以供阅读,但是当我查询时

   mysql_query("LOCK TABLES unique_voucher READ");

into the database it allows me to read only this table but not the other tables.进入数据库它允许我只读取这个表而不是其他表。

Is it possible to lock this particular table ONLY and does not allow the users to query this table until some process is finished?是否可以仅锁定此特定表并且在某些过程完成之前不允许用户查询此表?

(this is because i am holding a counter and if a lot of users connect at once they can get ambiguous numbers, so until one user is ready with once process I want to lock the reading for a table) (这是因为我拿着一个柜台,如果很多用户同时连接,他们可能会得到不明确的数字,所以在一个用户准备好一次进程之前,我想锁定表格的读数)

If you share the same DB connection for all users, one possible solution is to lock all other tables, except the one you want.如果您为所有用户共享相同的数据库连接,一种可能的解决方案是锁定所有其他表,您想要的表除外。 The locked tables will be available.锁定的表将可用。

From http://dev.mysql.com/doc/refman/5.0/en/lock-tables.html :来自http://dev.mysql.com/doc/refman/5.0/en/lock-tables.html

"A session that requires locks must acquire all the locks that it needs in a single LOCK TABLES statement. While the locks thus obtained are held, the session can access only the locked tables . For example, in the following sequence of statements, an error occurs for the attempt to access t2 because it was not locked in the LOCK TABLES statement:" “需要锁的 session 必须在单个 LOCK TABLES 语句中获取它需要的所有锁。在持有这样获得的锁的同时,session 只能访问锁定的表。例如,在以下语句序列中,错误尝试访问 t2 时发生,因为它没有在 LOCK TABLES 语句中锁定:"

mysql> LOCK TABLES t1 READ;
mysql> SELECT COUNT(*) FROM t1;
+----------+
| COUNT(*) |
+----------+
|        3 |
+----------+
mysql> SELECT COUNT(*) FROM t2;
ERROR 1100 (HY000): Table 't2' was not locked with LOCK TABLES

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM