mariadb-lock-tables

Guides correct use of MariaDB LOCK TABLES statements and GET_LOCK named advisory locks.

28|115|Updated Jan 28, 2025
One-click install
npx skills add https://github.com/mariadb-corporation/mariadb-docs --skill mariadb-lock-tables-mariadb-corporation
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: mariadb-lock-tables
Source: https://github.com/mariadb-corporation/mariadb-docs/tree/main/agent-skills/granular/statements/mariadb-lock-tables
Command: npx skills add https://github.com/mariadb-corporation/mariadb-docs --skill mariadb-lock-tables-mariadb-corporation

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Explicit locking in MariaDB behaves differently than most developers (and LLMs) assume: LOCK TABLES implicitly commits transactions, aliases must be locked separately, and GET_LOCK semantics changed across versions. This Skill prevents errors like 'Table was not locked with LOCK TABLES', lock-wait timeouts, and broken transaction/lock interactions when writing or reviewing locking code. ## Core Features & Use Cases - LOCK TABLES / UNLOCK TABLES guidance: Covers READ, READ LOCAL, WRITE, LOW_PRIORITY WRITE, WRITE CONCURRENT, and WAIT n/NOWAIT syntax, including alias-matching rules, implicit-commit behavior, and InnoDB's innodb_table_locks requirement. - Named advisory locks: Documents GET_LOCK, RELEASE_LOCK, RELEASE_ALL_LOCKS, IS_FREE_LOCK, and IS_USED_LOCK, including multi-lock-per-connection semantics since 10.0.2 and transaction independence. - Use Case: When reviewing code that mixes START TRANSACTION with LOCK TABLES, the Skill flags that LOCK TABLES implicitly commits the transaction and recommends SELECT ... FOR UPDATE or GET_LOCK instead. ## Quick Start Ask the AI to review your MariaDB locking code or explain how to implement an application-level mutex with GET_LOCK.

Frequently Asked Questions about mariadb-lock-tables

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I use LOCK TABLES in MariaDB?▼

Use LOCK TABLES tbl_name [AS alias] lock_type with READ, READ LOCAL, WRITE, LOW_PRIORITY WRITE, or WRITE CONCURRENT, optionally adding WAIT n or NOWAIT. You must lock every table and alias your session will reference, and UNLOCK TABLES releases all held locks.

How does GET_LOCK work in MariaDB?▼

GET_LOCK(str, timeout) acquires a server-wide named advisory lock, returning 1 on success, 0 on timeout, and NULL on error. Since MariaDB 10.0.2 a connection can hold multiple named locks simultaneously, and locks release only via RELEASE_LOCK, RELEASE_ALL_LOCKS, or connection close.

Why do I get 'Table was not locked with LOCK TABLES' error?▼

ERROR 1100 occurs when a statement references a table or alias not included in the LOCK TABLES clause. If you query t1 AS a, you must lock t1 AS a explicitly; locking the bare table name does not cover aliased references.

Can I use LOCK TABLES inside a transaction in MariaDB?▼

No. LOCK TABLES implicitly commits any active transaction before acquiring locks, and starting a new transaction releases all table locks. For transactional workflows, use SELECT ... FOR UPDATE row locking or GET_LOCK instead.

Does GET_LOCK get released by COMMIT or ROLLBACK?▼

No, named locks are independent of transactions. COMMIT and ROLLBACK never release them; locks are released only by RELEASE_LOCK, RELEASE_ALL_LOCKS, or the connection ending normally or abnormally.

When should I avoid LOCK TABLES in MariaDB?▼

Avoid LOCK TABLES in stored procedures (it errors at creation), on Galera cluster nodes, and for serializing rows inside transactional workflows. It is also unsafe for statement-based replication; prefer row locking or GET_LOCK in those cases.