Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6965869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:07:02+00:00 2026-05-27T16:07:02+00:00

The following sproc attempts to insert a row into a table and generate a

  • 0

The following sproc attempts to insert a row into a table and generate a random ID, which is used for a PK on the corresponding table. Collisions with randomly generated IDs are handled in the catch block, where the procedure is retried/called again. Now, this takes a long time and causes deadlocks, because locks are kept for an extended period of time. Is there a way to release deadlocks immediately before retrying, so that there is a short window when other threads can succeed in locking the PK index?


CREATE PROCEDURE addPerson
    (
        @FirstName nvarchar(100),
        @LastName nvarchar(100)
    )
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @PersonId int

    -- generate random PersonId
    -- this sproc can generate ids that already exist in the table
    EXEC generateRandomPersonId @PersonId=@PersonId OUTPUT

    BEGIN TRY       
        INSERT INTO [dbo].[Persons] 
        (
            PersonId,FirstName,LastName
        )
        VALUES 
        (
            @PersonId,@FirstName,@LastName
        )

    BEGIN CATCH
        -- 
        -- HOW TO RELEASE LOCKS HERE that are still held
        -- for the previous INSERT statement?
        --

        DECLARE @ErrorNumber int, @ErrorMessage nvarchar(2048)
        SELECT  @ErrorNumber=ERROR_NUMBER(), 
                @ErrorMessage=ERROR_MESSAGE()

        -- if a race condition happened and 
        -- PersonId happened to be picked already, retry all over again
        IF (@ErrorNumber = 2601 OR @ErrorNumber = 2627 AND CHARINDEX(N'PK_Persons_PersonId', @ErrorMessage) > 0)
            BEGIN
               --
               -- RETRYING HERE participates in a high possibility and 
               -- occurrence of deadlocks
               -- 
               EXEC addPerson @FirstName,@LastName
            END
            ELSE 
               -- some other error, rethrow it
               EXEC rethrowError
            END
    END CATCH
END
GO
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:07:02+00:00Added an answer on May 27, 2026 at 4:07 pm

    A process does not block on its own locks. Since a call to a stored procedure runs in the same process, it’s impossible for the second insert to wait for a lock from the first insert.

    Could you post a deadlock graph? That shows a lot of information on the blocking processes.

    As a quick fix, you could search for a free ID in a loop, which would avoid most (but not all) possible clashes:

    while 1=1
        begin
        EXEC generateRandomPersonId @PersonId=@PersonId OUTPUT
        if not exists (select * from Persons where PersonId = @PersonID)
            break
        end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my SPROC a table named #temp1 contains the following columns: #temp1 (StoreId, StoreDesc,
I have the following Query: create table #Result (Reward varchar(40), Value MONEY); insert #Result
Following up on this question, I'm working on a large Delphi 7 codebase which
Following is some obviously-defective code for which I think the compiler should emit a
I have a ms-sql table with the following structure: Name nvarchar; Sign nvarchar; Value
I have a SqlServer2005 table Group similar to the following: Id (PK, int) Name
I've got the following query (run in an sproc): DECLARE @BrandId uniqueidentifier SELECT *
I have to following sproc: USE [CW] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER
I'm trying to build something like the following sproc. However I can't seem to
I need help please with writing a sproc, it takes a table-valued parameter @Locations,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.