Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SQL Server Error 1223: Cannot Release Application Lock

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

SQL Server error 1223 (“Cannot release the application lock … because it is not currently held by the database principal”) means the session tried to release an application lock that it does not own. The fix is to release the lock from the same session and principal that acquired it, or to let the owning session release it. In practice: check sys.dm_tran_locks and sys.dm_exec_sessions to find the real owner, then either run sp_releaseapplock in that session or end that session so its locks are freed automatically. Do not try to force-release another principal’s lock with a different login.

What You'll Need

  • SQL Server Management Studio (SSMS) or Azure Data Studio connected to the instance.
  • Permission to view server state (for example, membership in sysadmin or VIEW SERVER STATE) to query the DMVs.
  • The application lock resource name and database name.
  • The login or session that originally acquired the lock.
  • Permission to run sp_releaseapplock in the target database (the same principal that acquired the lock, or a member of db_owner).

Step-by-Step

  1. Reproduce the error and capture the message. Run the statement that fails, or check the application log. Note the exact resource name, the database, and the principal named in the error. Error 1223 is raised by sp_releaseapplock when the current session does not hold a lock with that resource name for that principal.
  2. Confirm which session owns the lock. In SSMS, open a new query window against the database and run:
    SELECT request_session_id, resource_type, resource_database_id, resource_description, request_mode, request_status FROM sys.dm_tran_locks WHERE resource_type = 'APPLICATION';
    The request_session_id is the session that holds the lock. The resource_description shows the lock name. Labels and column names can differ slightly by SQL Server version, so verify against your version’s documentation if a column is missing.
  3. Map the session to a login and database. Run:
    SELECT s.session_id, s.login_name, s.program_name, s.host_name, DB_NAME(s.database_id) AS dbname FROM sys.dm_exec_sessions s WHERE s.session_id = <session_id>;
    This tells you which application or user owns the lock and whether it is still connected.
  4. Release the lock from the owning session. In the session that acquired the lock, run:
    EXEC sp_releaseapplock @Resource = N'<lock_name>', @DbPrincipal = N'<principal>', @LockOwner = 'Session';
    Use @LockOwner = 'Transaction' only if the lock was acquired with @LockOwner = 'Transaction'. The @DbPrincipal value must match the principal that acquired the lock, usually public or the application’s database user.
  5. If the owning session is gone or stuck, end it. If the session is idle and holding the lock, you can terminate it with KILL <session_id>;. This frees session-owned application locks automatically. Warn users first: killing a session rolls back its open transaction and can interrupt work. Do not kill system sessions (session_id 50 and below).
  6. Verify the lock is gone. Re-run the query from step 2. The application lock row for that resource should disappear. If it remains, the owner is still connected or the lock was acquired under a different principal name.
  7. Fix the application code. Ensure every sp_getapplock call has a matching sp_releaseapplock in the same session and with the same @Resource, @DbPrincipal, and @LockOwner values. If the lock is transaction-owned, release it before commit or rollback; if session-owned, release it before the connection closes or is returned to the pool.
  8. Test the corrected path. Run the application workflow again and confirm no error 1223 appears. Check that locks are released promptly and that no orphaned locks accumulate in sys.dm_tran_locks.

Common Mistakes

  • Releasing with a different @DbPrincipal than the one used to acquire the lock. The principal name is part of the lock identity, so a mismatch raises 1223.
  • Using @LockOwner = 'Session' to release a lock acquired with @LockOwner = 'Transaction', or the reverse.
  • Assuming the lock is held by the current session when it is actually held by a pooled connection or a different application instance.
  • Killing a session without checking whether it has an open transaction; this can roll back work and cause data loss or long recovery.
  • Releasing a lock twice. A second release in the same session raises 1223 because the lock is no longer held.

Tips

  • Standardize lock names and principal names in code so acquisition and release always match.
  • Use transaction-owned locks for short critical sections and session-owned locks only when the lock must span multiple transactions.
  • Log the session ID and lock name when acquiring and releasing, so troubleshooting is faster.
  • Monitor application locks periodically with sys.dm_tran_locks to catch leaks before they block users.
  • Keep connection pooling in mind: a session-owned lock can outlive the request if the connection is reused.

Troubleshooting

If the lock still appears after release, confirm you are connected to the correct database and that the resource name matches exactly, including case and spaces. Check whether the lock is held by a different principal by querying sys.dm_tran_locks and joining to sys.dm_exec_sessions. If the owning session is a background or system session, do not kill it; investigate the application or service that created it. If sp_releaseapplock returns a different error, such as a permission error, verify the login has execute permission on the procedure and is the same principal that acquired the lock. If the session cannot be found, the lock may have been acquired in a transaction that is still open in another connection; look for open transactions with sys.dm_tran_active_transactions and sys.dm_tran_session_transactions. As a last resort, restarting the application that owns the connection will close the session and release session-owned locks, but confirm no critical transaction is in progress first. If the problem recurs, review the code path that acquires the lock and ensure every exit path releases it, including error handlers.

Frequently Asked Questions

What causes SQL Server error 1223?

The session calling sp_releaseapplock does not hold an application lock with the specified resource name for the specified database principal. It is usually a mismatch in session, principal, or lock owner.

Can I release another session’s application lock?

No. Application locks must be released by the session that acquired them, or freed by ending that session. Use KILL only after confirming it is safe.

Does closing the connection release the lock?

Session-owned application locks are released when the session ends. Transaction-owned locks are released when the transaction commits or rolls back. With connection pooling, the session may persist after the application closes its connection object.

How do I find who holds an application lock?

Query sys.dm_tran_locks where resource_type = 'APPLICATION', then join request_session_id to sys.dm_exec_sessions to see the login and program name.

What is the difference between session and transaction lock owners?

A session-owned lock lasts until released or the session ends. A transaction-owned lock is tied to the current transaction and is released at commit or rollback.

Can error 1223 happen after a successful release?

Yes, if the code releases the same lock twice or if a different principal attempts the second release. Check the code path for duplicate release calls.