Smart Systems for Home

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

SQL Server Error 1226: Invalid Lock Owner Fix

· 6 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 1226 ("An invalid application lock owner was passed") means the session or transaction that tried to release, query, or change an application lock is not the same one that acquired it. The fix is to make sure the lock is released by the exact same connection and, where relevant, the same transaction that created it. In practice that means checking your sp_getapplock and sp_releaseapplock calls use a consistent @LockOwner value, running them on the same connection, and wrapping transaction-owned locks in a transaction that stays open until the release call. This guide walks through diagnosing and correcting the mismatch.

What You'll Need

  • Access to SQL Server Management Studio (SSMS), Azure Data Studio, or another client that can run T-SQL.
  • Permission to execute sp_getapplock and sp_releaseapplock (members of the public role can execute them by default, but the database user needs the appropriate permissions).
  • The name of the application lock resource and the code or stored procedure that acquires and releases it.
  • Ability to view session IDs (for example, @@SPID) and transaction state (@@TRANCOUNT) on the connection running the code.

Step-by-Step

  1. Reproduce the error and capture the context. Run the failing code in a test environment. Note the exact statement that returns error 1226. In SSMS, the Messages tab shows the error number and the procedure or line where it occurred. If the code is inside a stored procedure, capture the parameters passed to it.
  2. Identify how the lock was acquired. Search your code for sp_getapplock. Look at the @LockOwner argument. It is either 'Transaction' (the default) or 'Session'. Write down which one is used and the exact @Resource name. The owner value must match on every later call that references the same lock.
  3. Check the release or query call. Find every sp_releaseapplock and any other procedure that inspects the lock (for example, a call that queries sys.dm_tran_locks or uses APPLOCK_MODE). Confirm the @LockOwner argument is identical to the one used at acquisition. A common cause is acquiring with 'Session' but releasing with the default 'Transaction', or the reverse.
  4. Confirm the same connection is used. Application locks are scoped to the database and the session (or transaction). If your application uses connection pooling, a release call may run on a different pooled connection than the one that acquired the lock. Check the connection string and the code path: acquisition and release must execute on the same open connection. In SSMS, run both statements in the same query window to test.
  5. Verify transaction state for transaction-owned locks. If @LockOwner = 'Transaction', the lock is tied to the current transaction. Run SELECT @@TRANCOUNT; before the release call. If the count is 0, the transaction has already committed or rolled back, and the lock no longer exists under that owner, which can produce error 1226 or a related failure. Ensure BEGIN TRANSACTION is still active when sp_releaseapplock runs, or let the transaction end to release the lock automatically.
  6. Check for nested procedure calls that change owner. A stored procedure may acquire a lock with one owner and a nested procedure may attempt to release it with another. Trace the call stack. If a wrapper procedure opens a transaction and an inner procedure uses 'Session', the owners differ. Standardize on one owner per lock resource across the codebase.
  7. Correct the code and retest. Align the @LockOwner values, keep acquisition and release on the same connection, and ensure the transaction is open when releasing a transaction-owned lock. Rerun the original scenario. The error should no longer appear.
  8. Add a guard for production. Wrap release calls in a check such as IF APPLOCK_MODE('public', @Resource, @LockOwner) <> 'NoLock' before calling sp_releaseapplock, using the same owner value. This avoids releasing a lock that is not held by the current owner and makes failures easier to diagnose.

Common Mistakes

  • Mixing 'Session' and 'Transaction' owners for the same resource name. The owner is part of the lock identity, so a mismatch is treated as an invalid owner.
  • Releasing a lock after the transaction has already committed. With transaction-owned locks, the lock is gone once the transaction ends.
  • Assuming connection pooling preserves session state. A release call may land on a different physical connection, so the session that owns the lock is not the one releasing it.
  • Using a different database context. Application locks are scoped to the current database; acquiring in one database and releasing in another will not find the lock.
  • Ignoring the return code from sp_getapplock. A negative return value means the lock was not granted, so a later release has nothing valid to release.

Tips

  • Use a consistent naming convention for @Resource (for example, include the object and operation) so acquisition and release are easy to match in code review.
  • Prefer 'Session' ownership for locks that must span multiple transactions, and 'Transaction' for locks that should end with the transaction.
  • Log @@SPID, @@TRANCOUNT, and the @LockOwner value around acquisition and release during troubleshooting.
  • Keep acquisition and release in the same stored procedure or the same unit of work where possible, to reduce the chance of owner drift.
  • Test with the same client and connection settings used in production, because pooling behavior can differ between environments.

Troubleshooting

If error 1226 persists after aligning the owner values, work through these checks in order. First, confirm the lock actually exists: query sys.dm_tran_locks filtered by the resource description and your session ID. If no row appears, the lock was never acquired or was already released. Second, verify the database context with DB_NAME() on the connection running the release. Third, check whether a trigger or nested procedure is issuing a release with a different owner. Fourth, if the application uses an ORM or connection pool, force a single connection for the test to rule out pooling. Fifth, review the return code from sp_getapplock; a value of -1 or -2 means the lock was not granted, so releasing it is invalid. If the error appears only under load, add logging around acquisition and release to capture the session and transaction state at the moment of failure. If none of these resolve it, isolate the smallest reproducible script that acquires and releases the lock and compare it line by line with the failing code.

Frequently Asked Questions

What does error 1226 mean exactly?

It means the session or transaction calling an application lock procedure is not the owner of the lock being referenced. The owner value or the session context does not match the one used when the lock was acquired.

Can I release a lock acquired with 'Session' owner using the default owner?

No. The default for @LockOwner is 'Transaction', so you must pass 'Session' explicitly to release a session-owned lock. Otherwise SQL Server sees a different owner and returns error 1226.

Does error 1226 affect other sessions?

No. It is raised for the session making the invalid call. Other sessions holding their own locks are unaffected, but a failed release can leave a lock held longer than intended, which may block others.

What happens if I never release a session-owned lock?

The lock is held until the session ends. If the connection stays open (for example, in a pool), the lock can persist and block other sessions that request the same resource.

Is error 1226 related to deadlocks?

Not directly. It is an ownership validation error, not a deadlock. However, a lock that is not released because of this error can contribute to blocking that looks like a deadlock symptom.

Can I check who owns an application lock?

You can query sys.dm_tran_locks for application lock rows and inspect the request session ID and resource description. The built-in function APPLOCK_MODE returns the lock mode for a given resource and owner, which helps confirm whether the current owner matches.