Redigera

Dela via


sp_releaseapplock (Transact-SQL)

Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance

Releases a lock on an application resource.

Transact-SQL syntax conventions

Syntax

sp_releaseapplock
    [ [ @Resource = ] N'Resource' ]
    [ , [ @LockOwner = ] 'LockOwner' ]
    [ , [ @DbPrincipal = ] N'DbPrincipal' ]
[ ; ]

Arguments

[ @Resource = ] N'Resource'

A lock resource name specified by the client application. @Resource is nvarchar(255), with a default of NULL. @Resource is binary-compared, thus is case-sensitive regardless of the collation settings of the current database.

The application must ensure that the resource is unique. The specified name is hashed internally into a value that can be stored in the SQL Server lock manager.

[ @LockOwner = ] 'LockOwner'

The owner of the lock, which is the @LockOwner value when the lock was requested. @LockOwner is varchar(32), with a default of Transaction. The value can also be Session. When the @LockOwner value is Transaction, by default or specified explicitly, sp_getapplock must be executed from within a transaction.

[ @DbPrincipal = ] N'DbPrincipal'

The user, role, or application role that has permissions to an object in a database. @DbPrincipal is sysname, with a default of public. The caller of the function must be a member of database_principal, dbo, or the db_owner fixed database role in order to call the function successfully.

Return code values

>= 0 (success), or < 0 (failure).

Value Result
0 Lock was successfully released.
-999 Indicates parameter validation or other call error.

Remarks

When an application calls sp_getapplock multiple times for the same lock resource, sp_releaseapplock must be called the same number of times to release the lock.

When the server shuts down for any reason, the locks are released.

Permissions

Requires membership in the public role.

Examples

The following example releases the lock associated with the current transaction on the resource Form1 in the AdventureWorks2022 database.

USE AdventureWorks2022;
GO

EXEC sp_getapplock @DbPrincipal = 'dbo',
    @Resource = 'Form1',
    @LockMode = 'Shared';

EXEC sp_releaseapplock @DbPrincipal = 'dbo',
    @Resource = 'Form1';
GO