SQL Server Error 1221: Fix Database Engine Release Issues
In this article
SQL Server Error 1221 occurs when the database engine attempts to release a lock or a resource but cannot do so because the resource is still being held or referenced by another process, often resulting in a deadlock or a timeout. To resolve this, you must identify the blocking session using system dynamic management views (DMVs) and terminate the offending process using the KILL command to free the locked resource.
What You'll Need
- SQL Server Management Studio (SSMS) installed on your workstation.
- A user account with sysadmin or server-level permissions (required to view system processes and execute the KILL command).
- Access to the SQL Server instance experiencing the error.
Step-by-Step
- Connect to the Instance: Launch SQL Server Management Studio (SSMS). In the Connect to Server dialog, enter your server name and authentication credentials. Ensure you are logging in as an account with sufficient privileges to manage sessions.
- Open a New Query Window: Once connected, click the "New Query" button in the toolbar. This allows you to run the diagnostic scripts needed to find the root cause of the lock.
- Identify the Blocking Session: You need to find which Session ID (SPID) is holding the lock that the engine is trying to release. Run the following query to see active blocks:
SELECT blocking_session_id, wait_type, wait_time, last_wait_type FROM sys.dm_exec_requests WHERE blocking_session_id > 0;
This query returns theblocking_session_id, which is the ID of the process preventing other operations from completing. - Analyze the Blocking Query: Before killing a process, it is critical to know what that process is doing to avoid data corruption or interrupting a critical system task. Use the
sys.dm_exec_sql_textfunction to see the actual SQL code being executed by the blocker:
DBCC INPUTBUFFER (blocking_session_id_here);
Replaceblocking_session_id_herewith the ID you found in the previous step. If the query is a long-running report or a backup, you may want to wait; if it is a hung application session, proceed to the next step. - Terminate the Blocking Process: Once you have confirmed that the session can be safely stopped, use the KILL command. This forces the database engine to roll back any uncommitted transactions associated with that session and release all held locks:
KILL blocking_session_id_here;
Again, replace the placeholder with the actual SPID. - Verify Resource Release: After executing the KILL command, run the blocking query from Step 3 again. The results should now be empty, indicating that the locks have been released and the engine can proceed with its operations.
- Monitor for Recurrence: Keep the Activity Monitor open (Right-click the server name in Object Explorer > Activity Monitor) to ensure that the error does not immediately return, which would suggest a systemic issue with your application's transaction logic.
Common Mistakes
One of the most frequent mistakes is killing a system process. SQL Server uses internal SPIDs for background tasks like checkpointing, log writing, and ghost cleanup. Attempting to kill these can lead to instability or unexpected restarts. Always check the sys.dm_exec_sessions view to ensure the is_user_process column is set to 1 before executing a KILL command.
Another common error is ignoring the rollback phase. When you kill a session, SQL Server does not simply "stop" the query; it must undo every change the session made since its last commit to maintain ACID compliance. If the session had been running a massive update for hours, the rollback process can take an equal amount of time. Users often mistake a "Killed/Rollback" status for a frozen server and restart the entire SQL service, which is dangerous and can lead to lengthy database recovery times upon startup.
Finally, many administrators attempt to fix Error 1221 by simply restarting the server. While this clears the locks, it does not solve the underlying cause—such as poor indexing or inefficient transaction handling—meaning the error will inevitably return under the same load conditions.
Tips
To prevent Error 1221 from recurring, implement the SET LOCK_TIMEOUT command in your application code. This prevents a session from waiting indefinitely for a lock, instead throwing a manageable error after a specified number of milliseconds, which allows the application to retry the operation gracefully.
Consider using the READ COMMITTED SNAPSHOT ISOLATION (RCSI) level. By enabling RCSI, readers do not block writers, and writers do not block readers. This significantly reduces the frequency of locking conflicts and the likelihood of the database engine struggling to release resources during high-concurrency periods. You can enable this via: ALTER DATABASE [YourDB] SET READ_COMMITTED_SNAPSHOT ON;
Always ensure your statistics are up to date. Outdated statistics can lead the SQL Server Query Optimizer to choose a table scan over an index seek. A table scan locks far more rows (or the entire table) than a seek, drastically increasing the chance of lock contention and Error 1221.
Troubleshooting
If the KILL command does not resolve the issue, check the status of the session using KILL [SPID] WITH STATUSONLY. If the output shows a percentage of rollback remaining, you must wait for the rollback to complete. Do not restart the server during this phase, as it will force the database into "Recovery" mode upon restart, making the database unavailable to all users until the rollback finishes.
If you find that the blocking is happening between two sessions (a classic deadlock), the SQL Server Deadlock Monitor should normally resolve this automatically by killing one of the sessions. If it isn't, check your trace flags. Ensure that Trace Flag 1222 is enabled to write deadlock information to the error log, which helps you identify exactly which two queries are fighting for the same resource.
If the error persists despite no active blocking sessions, check for "orphaned distributed transactions." If your environment uses the Microsoft Distributed Transaction Coordinator (MSDTC), a transaction may be held open on a remote server, keeping a lock active on your local instance. Open the Component Services console (dcomcnfg), navigate to Component Services > Computers > My Computer > Distributed Transaction Coordinator > Local DTC, and check the Transaction List for any hung transactions that need to be manually aborted.
Frequently Asked Questions
Does Error 1221 mean my data is corrupted?
No, Error 1221 is a concurrency and locking issue, not a data corruption issue. It indicates a failure to release a lock, which affects availability and performance, but it does not inherently damage the underlying data pages.
Can I prevent this by increasing server RAM?
Generally, no. Adding RAM helps with memory pressure and caching, but locking is a logical mechanism used to ensure data integrity. If your queries are designed to lock entire tables, more RAM will not stop those locks from occurring.
Is it safe to use the NOLOCK hint to avoid this?
Using WITH (NOLOCK) prevents the query from requesting a shared lock, which avoids blocking others. However, it allows "dirty reads," meaning your query might read data that is in the middle of being updated and is later rolled back, leading to inaccurate reports.
Why does this happen more often during backups?
Backups require certain locks to ensure the consistency of the data being copied. If a long-running transaction is holding a lock on a system table or a large data page, the backup process or the engine's attempt to manage the backup locks can trigger this error.
What is the difference between a block and a deadlock?
A block occurs when Session A holds a lock and Session B waits for it. A deadlock occurs when Session A waits for a lock held by Session B, and Session B simultaneously waits for a lock held by Session A. Error 1221 is typically associated with the struggle to resolve these states.