Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql table lock aliasing not working

Tags:

mysql

locking

I have a scheduling table which I need to lock then read from. I am currently trying to do this:

LOCK TABLES db.schedule AS j_read READ;

SELECT * FROM db.schedule as j_read;

UNLOCK TABLES;

When I run this I get the error:

Error Code: 1100. Table 'j_read' was not locked with LOCK TABLES

I've gone over the mysql table locks documentation mysql docs but i cant figure our why this is not working for me. if i don't alias anything it works like this

LOCK TABLES db.schedule READ;

SELECT * FROM db.schedule;

UNLOCK TABLES;

Any ideas why this is not working or how to do this properly?

like image 264
Jake Avatar asked Mar 04 '26 23:03

Jake


2 Answers

I found the way to make this work. Need to obtain a lock for both the alias name and the table itself

LOCK TABLES db.schedule READ,db.schedule AS j_read READ;

SELECT * FROM db.schedule as j_read;

UNLOCK TABLES;
like image 58
Jake Avatar answered Mar 08 '26 22:03

Jake


Look at this chunk of documentation, you still need to select from table but reference your lock alias. You're looking for last line.

mysql> LOCK TABLE t AS myalias READ;
mysql> SELECT * FROM t;
ERROR 1100: Table 't' was not locked with LOCK TABLES
mysql> SELECT * FROM t AS myalias;
like image 23
Konstantin Avatar answered Mar 08 '26 22:03

Konstantin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!