Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get locked tables in mysql query

Tags:

Is there any way via a MySQL query to get the locked tables? I have a C# threading application running and there are bunch of tables getting locked in the app.

I need to see the locked tables and analyze the code that could be locking it.

like image 794
Sharpeye500 Avatar asked Jul 12 '10 17:07

Sharpeye500


People also ask

Does SELECT query lock table in MySQL?

SELECTs do not normally do any locking that you care about on InnoDB tables. The default transaction isolation level means that selects don't lock stuff.

How do you know if a table is locked?

SHOW OPEN TABLES WHERE `Table` LIKE '%[TABLE_NAME]%' AND `Database` LIKE '[DBNAME]' AND In_use > 0; to check any locked tables in a database. Show activity on this post. You can use SHOW OPEN TABLES to show each table's lock status.

What is read lock in MySQL?

MySQL Locks: Read LocksIf the session holds the READ lock on a table, they cannot perform a write operation on it. It is because the READ lock can only read data from the table. All other sessions that do not acquire a READ lock are not able to write data into the table without releasing the READ lock.

What is the lock table query?

A locked table remains locked until you either commit your transaction or roll it back, either entirely or to a savepoint before you locked the table. A lock never prevents other users from querying the table. A query never places a lock on a table.


1 Answers

Use:

SHOW OPEN TABLES 

An check whether the column In_use is greater than 0. In that case, the table is locked.

Examples

  • List of locked tables:

    show open tables WHERE In_use > 0

  • Check whether the table tb_employees is locked or not:

    show open tables WHERE Table LIKE 'tb_employees' AND In_use > 0

From the official documentation:

In_use

The number of table locks or lock requests there are for the table. For example, if one client acquires a lock for a table using LOCK TABLE t1 WRITE, In_use will be 1. If another client issues LOCK TABLE t1 WRITE while the table remains locked, the client will block waiting for the lock, but the lock request causes In_use to be 2. If the count is zero, the table is open but not currently being used. In_use is also increased by the HANDLER ... OPEN statement and decreased by HANDLER ... CLOSE.

like image 139
Cristian Avatar answered Sep 28 '22 06:09

Cristian