detecting locked tables mysql (locked by LOCK TABLE)

I would like to know whether there is an option to detect locked tables in mysql or not. I mean locked by LOCK TABLE table WRITE/READ command?

share|improve this question
 
    
Why don't you unlock the table first ? –  David V. Mar 23 '10 at 13:07
6  
@David V.: I guess because he needs to know it is locked first? Or maybe he want's to avoid 'touching' the table when/if it is locked? –  lexu Mar 23 '10 at 13:11
    
O_O Ooooh my bad, I read that as "delete" (and deleting in the title) locked tables. –  David V. Mar 23 '10 at 13:42

5 Answers

You can't for non-named locks!

More info: http://forums.mysql.com/read.php?21,222363,223774#msg-223774

Not unless you use an advisory lock first. You might also want to look into using INSERT DELAYED instead.

share|improve this answer
 

You can use SHOW OPEN TABLES to show each table's lock status. More details on the command's doc page are here.

share|improve this answer
 

Use SHOW OPEN TABLEShttp://dev.mysql.com/doc/refman/5.1/en/show-open-tables.html

You can do something like this

SHOW OPEN TABLES WHERE `Table` LIKE '%foo%' AND Database LIKE '[DBNAME]' AND In_use > 0;

to check any locked tables in a database.

share|improve this answer
 
3  
Note: I can only get this query to work if I use `Database` instead of just Database –  rinogo Feb 11 at 23:06 

You can create your own lock with GET_LOCK(lockName,timeOut)

If you do a GET_LOCK(lockName, 0) with a 0 time out before you lock the tables and then follow that with a RELEASE_LOCK(lockName) then all other threads performing a GET_LOCK() will get a value of 0 which will tell them that the lock is being held by another thread.

However this won't work if you don't have all threads calling GET_LOCK() before locking tables. The documentation for locking tables is here

Hope that helps!

share|improve this answer
 
    
this only works for named locks what about non named locks? –  Marcin Apr 5 '10 at 12:32
    
You can't for non named locks –  Giles Smith Apr 6 '10 at 16:31

This article describes how to get information about locked MySQL resources. mysqladmin debugmight also be of some use.

share|improve this answer
 
1  
its fine but what about MyISAM being locked ? cheers –  Marcin Mar 23 '10 at 13:56
    
Sorry, can't help you there. –  Tomislav Nakic-Alfirevic Mar 23 '10 at 15:02
posted @   seasonzone  阅读(360)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
点击右上角即可分享
微信分享提示