Get Exclusive Application
Lock:
DECLARE @ProcResult int = 0;
-- Get an exclusive application lock on the
package execution resource, to prevent other executions of the same package
from doing the same
EXEC @ProcResult = sp_getapplock @Resource = @PackageResourceName,
@LockMode = 'Exclusive',
@LockOwner = 'Session',
@LockTimeout = 0; -- Do not wait for the lock
IF @ProcResult < 0
RAISERROR('Failed to acquire application lock on %s.
sp_getapplock returned %d', 16, 1, @PackageResourceName, @ProcResult);
Here @PackageResourceName is
a string specifying a name that identifies the lock resource. The application
must ensure that the resource name is unique. The specified name is hashed
internally into a value that can be stored in the SQL Server lock manager
Release Exclusive Application
Lock:
DECLARE @ProcResult int = 0;
-- Release a previously acquired exclusive
application lock on the package execution resource
EXEC @ProcResult = sp_releaseapplock @Resource = @PackageResourceName,
@LockOwner = 'Session';
IF @ProcResult < 0
RAISERROR('Failed to release application lock on %s. sp_releaseapplock
returned %d', 16, 1, @PackageResourceName, @ProcResult);
Make sure the "RetainSameConnection" property on
the Database connection manager ( used to execute the above commands) is set to True.
No comments:
Post a Comment