May 15, 2012

sp_MSgetmakegenerationapplock (Transact-SQL MetaData) Definition

Please note: that the following source code is provided and copyrighted by Microsoft and is for educational purpose only.
The meta data is from an SQL 2012 Server.

I have posted alot more, find the whole list here.

Goto Definition or MetaData

Definition:

sys.sp_MSgetmakegenerationapplock()

MetaData:

   
create procedure sys.sp_MSgetmakegenerationapplock
@head_of_queue int OUTPUT
as
set nocount on

declare @retcode smallint
declare @lock_resource nvarchar(255)
declare @DbPrincipal sysname

-- Security check
exec @retcode = sys.sp_MSrepl_PAL_rolecheck
if (@retcode <> 0) or (@@error <> 0)
return 1

select @retcode = 0
select @head_of_queue = 0

select @lock_resource = N'MSinternal_makegeneration_inprogress' +
convert(nvarchar(11), db_id())
if exists (select * from sys.database_principals where name=N'MSmerge_PAL_role' and type = 'R')
select @DbPrincipal = N'MSmerge_PAL_role'
else
select @DbPrincipal = N'db_owner'

-- If someone else is making generations / has just made one, exit so
-- that we won't deadlock
exec @retcode = sp_getapplock @Resource = @lock_resource,
@LockMode = N'Exclusive',
@LockOwner = N'Session',
@LockTimeout = 0, -- do not wait at all.
@DbPrincipal = @DbPrincipal

if @@error <> 0 or @retcode < 0
begin
-- If the previous applock request timed out, then try again, this time using the default timeout.
-- Note that even though the -1 lock timeout means indefinite timeout, the query timeout set on the
-- session overrides the lock timeout. So it's okay even if the default @@lock_timeout is -1.
if (@retcode = -1)
begin
exec @retcode = sp_getapplock @Resource = @lock_resource,
@LockMode = N'Exclusive',
@LockOwner = N'Session',
@DbPrincipal = @DbPrincipal

-- If we succeeded to get the lock, set @lock_acquired to 1 so that the exit code releases the lock.
if (@retcode >= 0)
begin
goto EXIT_PROC
end
end
end
else
begin
select @head_of_queue = 1
end

EXIT_PROC:
return @retcode

No comments:

Post a Comment

Total Pageviews