Issue:
A database lock occurs when a query or transaction blocks or stalls due to a lock conflict. This may appear in code execution or log messages, for example: Raw codesqlite3.OperationalError: database is locked
Root Cause:
Common causes of database locks include:- Concurrent data access — multiple processes try to read and write the same data simultaneously, requiring the database to serialize access
- Long-running transactions — a transaction holds locks for an extended period, blocking other processes
- Resource contention — multiple processes compete for limited disk or memory resources
- Deadlocks — two or more processes each wait for the other to release a lock, creating a cycle that never resolves
- Hardware or software failures — locks persist beyond a failure even after the failure itself resolves
Resolution:
MySQL Error messages: Raw codeDeadlock found when trying to get lock Lock wait timeout exceeded; try restarting transaction
Fix steps:
- Run
SHOW PROCESSLISTto identify the process holding the lock.
SHOW PROCESSLIST;
- Terminate the blocking process using its process ID.
KILL <process_id>;
- If an open transaction is causing the lock, roll it back.
ROLLBACK;
SQL Server
Error message:
Raw code
Transaction (Process ID) was deadlocked on lock resources with another process and was chosen as the victim of the deadlock. Restart the transaction.
Fix steps:
- Run
sp_who2to identify the blocking process.
EXEC sp_who2;
- Terminate the blocking process using its process ID.
KILL <process_id>;
- If an open transaction is causing the lock, roll it back.
ROLLBACK;
SQLite
Error message:
Raw code
sqlite3.OperationalError: database is locked
Fix steps:
- Commit any open transaction.
conn.commit()
- Close the connection to release the lock.
conn.close()
- If multiple connections exist, close each one before reopening. Use the
sqlite3module in Python to manage open connections.
# Example: close all connections before re-querying for conn in open_connections: conn.close()
MongoDB
Error messages:
Raw code
Error: couldn't execute operation: couldn't execute command. CommandFailed: Transaction 12 is being rolled back - lock was not available
Fix steps:
- If a long-running operation holds the lock, wait for it to finish.
- If the operation is stuck, terminate it using its operation ID.
db.killOp(<op_id>)
- As a last resort, restart the MongoDB server if a bug or system issue is causing the lock.
- ps — lists running processes and shows which processes hold locks
- lsof — lists all open files, including locked ones, and shows which process locks each file
- strace — traces system calls and signals for a process, helping identify the root cause of a lock
- fuser — shows which processes use a specific file or file system, including those holding locks
Notes/Information:
- Design your schema and queries to minimize contention and reduce the need for locks.
- Experiment with different isolation levels to prevent transactions from conflicting with each other.
- Address locks promptly — persistent locks degrade database performance.
References/Internal Records:
- Public KB Articles: