9

I have a small curiosity about MySQL table locks.

Say I want to lock two tables. I execute this command:

LOCK TABLES table1 WRITE, table2 WRITE

And then I checked if the tables have indeed been locked by executing:

SHOW OPEN TABLES IN mydatabase WHERE In_use > 0

I have noticed tho that if I run two lock commands sequentitally for example:

LOCK TABLES table1 WRITE
LOCK TABLES table2 WRITE

And then check which tables are locked using the same command only table2 is marked as locked. Why is this so?

Thanks

4

2 回答 2

10

LOCK TABLES不是事务安全的,它会在尝试锁定表之前隐式提交任何活动事务。

所以,在第一种情况下,你有一个事务持有 2 个表被锁定,在第二个只有一个,因为LOCK TABLES table1 WRITE已经提交

于 2013-02-08T16:24:44.153 回答
0

锁定表会先解锁当前会话锁定的所有表,然后再执行指定的锁定。所以锁定表 2 的调用是解锁表 1。

于 2015-11-19T23:09:57.587 回答