How to Fix MySQL Error 1099 (ER_TABLE_NOT_LOCKED_FOR_WRITE)
You get mysql error 1099 when a session tries to change a table that holds a read lock. To fix it, release the current read lock or request a write lock before changing data. The database engine uses the symbol ER_TABLE_NOT_LOCKED_FOR_WRITE and SQLSTATE HY000 for this failure. It tells you that the target table was locked with a read lock and cannot be updated. This rule protects data integrity when multiple sessions read from the same table at once. What causes mysql error 1099 in your datab
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You get mysql error 1099 when a session tries to change a table that holds a read lock. To fix it, release the current read lock or request a write lock before changing data.
The database engine uses the symbol ER_TABLE_NOT_LOCKED_FOR_WRITE and SQLSTATE HY000 for this failure. It tells you that the target table was locked with a read lock and cannot be updated. This rule protects data integrity when multiple sessions read from the same table at once.
What causes mysql error 1099 in your database
Database sessions use explicit table locks to control how queries read and write shared data. When a session sets a read lock, any user can read from that specific table. Still, no session can insert, update, or delete rows until the lock ends. Even the session that asked for the read lock cannot write to that table.
If that same session tries to modify a row, the engine stops the query right away. It returns the 1099 error code to prevent silent data corruption during read operations. This issue often happens during manual maintenance tasks, backup scripts, or batch data exports. A script might lock tables to read a consistent snapshot and then try to log progress. If the log table is also under a read lock, the write attempt fails.
Connection pooling in an app can also trigger this problem across separate requests. One worker might set a read lock on a session and fail to clear it. When the pool hands that session to another worker, the next write query will fail. Modern transactional engines handle most concurrency with row locks instead of full table locks. Still, legacy apps and migration scripts often use explicit table locks and hit this wall.
How to diagnose and fix the read lock failure
Start by checking where your session or app sets explicit locks before running write queries. Look for scripts or app code that run read locks and then try to change rows. You must decide whether the session needs a read-only view or needs to update data.
If the session only needs to read a clean snapshot, move the write query elsewhere. Run the update or insert after you release the active table locks in that session. For backup or export jobs, use transaction isolation instead of locking tables by hand. Transactions let you read a consistent view of data while other sessions continue to write.
MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
If the session truly needs to lock the table and change data, use a write lock. A write lock lets your current session read and change the table while blocking others. Always release all locks as soon as the batch task finishes its work. Never fix this issue by granting broad admin roles to app accounts or skipping checks.
Preventing lock errors in production workflows
Ad-hoc queries run by engineers during incidents are a common source of lock conflicts. An engineer might lock a table to check records and forget to release the session. Giving engineers direct database credentials makes it hard to track or limit these lock commands.
Teams should set up clear rules for how users and scripts run queries in production. Separate read-only diagnostic sessions from sessions that have permission to change production tables. When a user only needs to inspect data, their session should enforce read-only behavior by default. You can also set up data masking to rewrite emails, phone numbers, or card numbers in results. For approved changes, use scoped sessions with strict timeouts so stale locks never linger.
Check MySQL queries and results on the wire
MySQL grants control who logs in. They do not check single queries or mask sensitive columns once a session starts. With hoop.dev, you run an open-source proxy, a sidecar for your MySQL, inline in the MySQL wire protocol. On every query an AI agent or human runs against production MySQL, RDS, or Aurora, hoop.dev decides to pass, mask, block, or hold it for review.
Multi-statement batches and prepared queries are split and checked one by one by hoop.dev guardrails, which refuse banned queries with a native MySQL error or hold risky ones for approval. On the response path, hoop.dev data masking rewrites string values in returned rows in memory, and every decision is logged. See the MIT-licensed project on GitHub.
Frequently asked questions
Why does my own session fail to write when it holds the lock?
A read lock is shared and strictly prevents any data changes from any session. Even the session that created the read lock must release it or use a write lock.
What happens if a different session tries to update a read-locked table?
Another session will wait in a queue until your read lock is released. Only the session that holds the read lock gets error 1099 right away.
How can I avoid table lock errors during database backups?
Use single-transaction backup modes on transactional tables instead of setting manual table read locks. This method takes a clean snapshot without blocking writes or causing lock errors.
Related guides
In this post