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

  • SEARCH
  • Home
  • 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 9274725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:25:11+00:00 2026-06-18T16:25:11+00:00

Posting the question and one answer here. Maybe somebody has better answer… It’s possible

  • 0

Posting the question and one answer here. Maybe somebody has better answer…

It’s possible to write code that triggers a deadlock even for a single user if the developer accidentally opens up a second connection to the database instead of reusing the an existing one (which might already have an open transaction). Certain O/RM and LINQ frameworks make this mistake easy.

Here is the scenario in pure SQL terms:

--Setup:
CREATE TABLE Vendors(VendorID int NOT NULL, VendorName nvarchar(100) NOT NULL, ExtraColumn int not null, CONSTRAINT PK_Vendors PRIMARY KEY CLUSTERED(VendorID))
GO
INSERT INTO Vendors(VendorID,VendorName,ExtraColumn) VALUES (1, 'Microsoft', 12345)
INSERT INTO Vendors(VendorID,VendorName,ExtraColumn) VALUES (2, 'Oracle', 12345)

--Connection 1:
BEGIN TRANSACTION
UPDATE Vendors SET ExtraColumn = 222 WHERE VendorID = 2

--Connection 2:
BEGIN TRANSACTION
SELECT VendorName FROM Vendors WHERE VendorID = 1

This code (and the c#/java/ORM/LINQ/whatever that generated it) might run happily in development/testing when the data is small, but suddenly deadlock when the data/memory profile changes in production and the lock escalates from row to page to table.

So how can I force lock escalation up to table level in my test environment so I can flush out any bugs like this (where the code opens up the 2nd connection)?

Lock escalation is completely controlled by the SQL Server database engine and it’s not possible to predict when it will escalate from row lock to table lock. I won’t rehash all the details which you can find here, but one in particular to note: “Lock escalation is triggered when…The number of locks in an instance of the Database Engine exceeds memory or configuration thresholds.” This means that it could be completely unrelated to my carefully-crafted code and perfectly-selected indices.

  • 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-06-18T16:25:12+00:00Added an answer on June 18, 2026 at 4:25 pm

    One approach is to:

    1. In the test environment, run a script like the following to disable row and page locking on all indexes (making it go directly to table lock)
    2. Run the tests
    3. Run the script again setting it back to normal locking
    --
    -- Script to disable/enable row & page locking on dev/test environment to flush out deadlock issues
    -- executes statement like this for each table in the database:
    -- ALTER INDEX indexname ON tablename SET ( ALLOW_ROW_LOCKS  = OFF, ALLOW_PAGE_LOCKS  = OFF )
    --
    -- DO NOT RUN ON A PRODUCTION DATABASE!!!!
    --
    set nocount on
    declare @newoption varchar(3)
    --------------------------------------------------------------------
    -- Change variable below to 'ON' or 'OFF' --------------------------
    --   'OFF' means row & page locking is disabled and everything 
    --      triggers a table lock
    --   'ON' means row & page locking is enabled and the server chooses
    --     how to escalate the locks (this is the default setting)
    set @newoption = 'OFF'
    --------------------------------------------------------------------
    
    DECLARE    @TableName varchar(300)
    DECLARE    @IndexName varchar(300)
    DECLARE @sql varchar(max)
    
    DECLARE inds CURSOR FAST_FORWARD FOR
    SELECT tablename, indname
    FROM (
        select top 100 percent
        so.name as tablename
             , si.indid
             , si.name as indname
             , INDEXPROPERTY( si.id, si.name, 'IsPageLockDisallowed') as IsPageLockDisallowed
             , INDEXPROPERTY( si.id, si.name, 'IsRowLockDisallowed') as IsRowLockDisallowed
        from   sysindexes si
        join sysobjects so on si.id = so.id
        where  si.status & 64 = 0
          and  objectproperty(so.id, 'IsMSShipped') = 0
          and si.name is not null
          and so.name not like 'aspnet%'
          and so.name not like 'auditLog%'
        order by so.name, si.indid
    ) t
    
    OPEN inds
    FETCH NEXT FROM inds INTO @TableName, @IndexName
    
    WHILE @@FETCH_STATUS = 0
    BEGIN
    
        SET @sql = 'ALTER INDEX [' + @IndexName + '] ON [dbo].[' + @TableName + '] SET ( ALLOW_ROW_LOCKS  = ' + @newoption + ', ALLOW_PAGE_LOCKS  = ' + @newoption +' )'
        PRINT @sql
        EXEC(@sql)
    
        FETCH NEXT FROM inds INTO @TableName, @IndexName
    END
    
    CLOSE inds
    DEALLOCATE inds
    
    
    PRINT 'Done'
    

    Other notes:

    • I got the core of the above script from somebody else’s article, but I’ve long-since forgotten where. Apologies for the lack of attribution.
    • Note that the above script will overwrite any existing custom lock escalation settings on your tables. You can check for these by first running the inner select (“SELECT TOP 100 PERCENT so.name…”) to see the existing settings.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Posting both question and (one) answer here to see if somebody has a better
This is my first time posting a question here, it has been a valuable
NOTE : Right before posting this question it occurred to me there's a better
It might be a long shot posting this question here but we will see.
I am again posting the same question. Please help me in this problem. Here
I am trying to integrated linkedIn using this question answer Posting LinkedIn message from
this is my first time posting here, I have a question which I have
First off, if there is a question/answer that solves my problem already then I
Possible Duplicate: Hide facebook app from search Okay, I know that this question was
After posting one of my most controversial answers here , I dare to ask

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.