Smart Systems for Home

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

SQL Server Error 1225: Invalid Application Lock Mode

· 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 1225 ("An invalid application lock mode was passed") means your code called sp_getapplock or sp_releaseapplock with a lock mode value that SQL Server does not recognize. The fix is to use one of the six valid, case-insensitive strings: 'Shared', 'Update', 'Exclusive', 'IntentShared', 'IntentExclusive', or 'SharedUpdate'. Anything else—a typo, a numeric code, a NULL, or a mode borrowed from a different locking API—triggers the error. This guide walks through diagnosing the failing call, correcting the mode, and preventing the problem from recurring.

What You'll Need

  • Access to the SQL Server instance where the error occurs, with permission to run the affected stored procedure or query.
  • SQL Server Management Studio (SSMS) or Azure Data Studio, or any client that can execute T-SQL and show messages.
  • The name of the stored procedure, trigger, or application code that calls sp_getapplock or sp_releaseapplock.
  • Permission to view the procedure definition (ALTER PROCEDURE or VIEW DEFINITION) so you can inspect the @LockMode parameter.

Step-by-Step

  1. Reproduce the error and capture the exact message. Run the application action or execute the procedure directly in SSMS. The error text will include the invalid mode value if it was passed as a string, or simply state that an invalid mode was passed if the value was numeric or NULL. Note the procedure name and line number from the error stack.
  2. Locate every call to sp_getapplock and sp_releaseapplock. Open the procedure definition with sp_helptext or Object Explorer. Search for both procedure names. Pay attention to the @LockMode parameter, which is the second positional argument in sp_getapplock and the first in sp_releaseapplock. Also check any dynamic SQL strings that build these calls.
  3. Compare the mode value against the valid list. The only accepted values are 'Shared', 'Update', 'Exclusive', 'IntentShared', 'IntentExclusive', and 'SharedUpdate'. Values are case-insensitive, so 'exclusive' is fine. Common mistakes include 'Excl', 'Read', 'Write', 'SharedExclusive', '1', or a variable that is NULL. If the mode comes from a variable, trace where that variable is set.
  4. Correct the mode in the code. Replace the invalid value with the closest valid mode. For read-only coordination use 'Shared'. For a write that must not run concurrently with other writers use 'Exclusive'. For a two-phase pattern where you read then upgrade, use 'Update' and then 'Exclusive'. 'IntentShared' and 'IntentExclusive' are for hierarchical locking scenarios and are rarely needed in application code. If the mode is supplied by the application, fix the application constant or configuration, not just the procedure.
  5. Check for NULL or missing parameters. If @LockMode is omitted in a call to sp_getapplock, SQL Server defaults to 'Exclusive' for sp_getapplock but requires an explicit mode for sp_releaseapplock. A NULL passed explicitly will raise error 1225. Add ISNULL or a default in the procedure signature, or ensure the caller always supplies a valid string.
  6. Verify the release call matches the acquire call. sp_releaseapplock must use the same lock mode that was used to acquire the lock. If you acquired with 'Shared' but release with 'Exclusive', you will get error 1225. Store the mode in a variable and reuse it for both calls.
  7. Test in a transaction. Application locks are scoped to the session and transaction. Run a small test: BEGIN TRAN; EXEC sp_getapplock @Resource='test', @LockMode='Exclusive', @LockOwner='Transaction'; EXEC sp_releaseapplock @Resource='test', @LockOwner='Transaction'; COMMIT; If this succeeds, the mode is valid and the original error came from a different call path.
  8. Deploy and monitor. After correcting the code, deploy to a test environment first. Run the application workflow that previously failed. Check the SQL Server error log and the application log for any remaining 1225 errors. If the error persists, search all modules for the invalid string using sys.sql_modules.

Common Mistakes

  • Using numeric lock codes. Some other database platforms use integers for lock modes. SQL Server application locks accept only the named strings listed above.
  • Assuming sp_releaseapplock has a default mode. Unlike sp_getapplock, sp_releaseapplock requires an explicit @LockMode. Omitting it or passing NULL causes error 1225.
  • Mismatching acquire and release modes. The release call must specify the same mode used to acquire the lock. Using a different valid mode still raises 1225.
  • Hardcoding modes in dynamic SQL. String concatenation can introduce trailing spaces or quotes that make the mode invalid. Use parameterized calls or sp_executesql with typed parameters.
  • Overlooking triggers and nested procedures. The failing call may be inside a trigger or a procedure called by the main procedure. Search all modules, not just the one named in the error.

Tips

  • Define the valid modes as constants in your application code to avoid typos and make future changes easier.
  • Use 'Shared' for read coordination and 'Exclusive' for write coordination. Reserve 'Update' for patterns where you need to read, decide, then upgrade to exclusive.
  • Always pair sp_getapplock with sp_releaseapplock in the same scope, and use a TRY...CATCH block to ensure release even on error.
  • Keep lock owner consistent: if you acquire with @LockOwner='Transaction', release with the same. Mixing 'Session' and 'Transaction' can cause unexpected behavior and errors.
  • Test application lock logic under concurrency, not just single-threaded, to catch mode mismatches early.

Troubleshooting

If error 1225 persists after correcting the obvious call, work through these checks in order. First, confirm the error is actually 1225 and not a different application lock error such as 1222 (lock request timeout). Second, search the entire database for the invalid mode string using SELECT OBJECT_NAME(object_id), definition FROM sys.sql_modules WHERE definition LIKE '%sp_getapplock%' OR definition LIKE '%sp_releaseapplock%'. Third, check for encrypted procedures: if WITH ENCRYPTION is used, you cannot view the definition and must rely on the application source or recreate the procedure without encryption in a test copy. Fourth, verify that the application is not connecting to a different database or instance than the one you fixed. Fifth, if the mode is supplied by a client library or ORM, check its documentation for the correct string format; some libraries expect an enum that maps to the SQL Server strings. Finally, if you cannot find the call, enable SQL Server Profiler or Extended Events to capture the exact statement and parameters at the moment of failure. Look for the RPC:Completed event and inspect the @LockMode parameter value.

Frequently Asked Questions

What are the valid lock modes for sp_getapplock?

The valid modes are 'Shared', 'Update', 'Exclusive', 'IntentShared', 'IntentExclusive', and 'SharedUpdate'. They are case-insensitive.

Why does sp_releaseapplock require a lock mode?

sp_releaseapplock requires the same mode used to acquire the lock so SQL Server can correctly match and release the specific lock. Omitting it or passing NULL raises error 1225.

Can I use a variable for @LockMode?

Yes, but the variable must contain one of the valid strings. If the variable is NULL or contains an invalid value, error 1225 occurs.

Does error 1225 mean the lock was not acquired?

Yes. The error occurs before any lock is taken, so no lock is held and no release is needed for that call.

Is error 1225 related to deadlocks?

No. Error 1225 is a parameter validation error, not a deadlock or timeout. Deadlocks produce error 1205, and lock timeouts produce error 1222.

Can I use sp_getapplock outside a transaction?

Yes, if you specify @LockOwner='Session'. The default is 'Transaction', which requires an active transaction. Using 'Transaction' without a transaction raises a different error.