USP_MKTSEGMENT_GETAPPLOCK

Acquires application resource locks for a segment and all of its selections, and conditionally acquire locks for all dependent selections for each selection of the segment.

Parameters

Parameter Parameter Type Mode Description
@SEGMENTID uniqueidentifier IN
@INCLUDEDEPENDENTSELECTIONS bit IN

Definition

Copy


CREATE procedure dbo.[USP_MKTSEGMENT_GETAPPLOCK]
(
  @SEGMENTID uniqueidentifier,
  @INCLUDEDEPENDENTSELECTIONS bit = 0
)
as
  set nocount on;

  declare @LOCKNAME nvarchar(255);
  declare @LOCKRESULT int;


  /*****************************************/
  /* Get an applock for the segment itself */
  /*****************************************/
  set @LOCKNAME = 'MKTSEGMENT:' + cast(@SEGMENTID as nvarchar(36));
  exec @LOCKRESULT = sp_getapplock @Resource=@LOCKNAME, @LockMode='Exclusive', @LockOwner='Session', @LockTimeout=3600000;
  if @LOCKRESULT < 0
    begin
      raiserror('A failure or timeout occurred while requesting an app lock for a segment.', 13, 1);
      return @LOCKRESULT;
    end


  /*******************************************************/
  /* Get applocks for all the selections of the segment, */
  /* and all the selections used inside these selections */
  /*******************************************************/
  declare @SELECTIONS table([ID] uniqueidentifier not null);
  declare @SELECTIONID uniqueidentifier;
  declare @QUERYDEFINITIONXML xml;

  --Grab the selections for the segment into a temp table, so when we are waiting for a lock, we don't lock other things with our cursor...

  insert into @SELECTIONS
    select [SELECTIONID]
    from dbo.[MKTSEGMENTSELECTION]
    where [SEGMENTID] = @SEGMENTID;

  --For each selection, get the applock and applocks for any dependent selections...

  declare SELECTIONCURSOR cursor local fast_forward for
    select [ID]
    from @SELECTIONS;

  open SELECTIONCURSOR;
  fetch next from SELECTIONCURSOR into @SELECTIONID;

  while (@@FETCH_STATUS = 0)
  begin
    --Get the applock for the selection...

    exec dbo.[USP_IDSETREGISTER_GETAPPLOCK] @SELECTIONID, @INCLUDEDEPENDENTSELECTIONS;

    fetch next from SELECTIONCURSOR into @SELECTIONID;
  end

  close SELECTIONCURSOR;
  deallocate SELECTIONCURSOR;

  return 0;