Table lock is not released after running stored procedure

Rachel 1 Reputation point
2020-11-13T04:53:52.087+00:00

I have a stored procedure doing DELETE, INSERT, SELECT on an existing table.
In my application(using a connection pool), after running this stored procedure, the DB connection is still there. When I run this stored procedure one more time, the DELETE statement is blocked, the wait type is LCK_M_U.
Why the 1st connection still holds the table lock after running this stored procedure?
How to make sure the table lock is released?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,685 questions
{count} votes

4 answers

Sort by: Most helpful
  1. m 4,271 Reputation points
    2020-11-13T07:41:30.02+00:00

    Hi @Rachel ,

    Why the 1st connection still holds the table lock after running this stored procedure?

    Maybe it is not be committed, try code commit tran will sove the issue.

    Verify it is blocked by this stored procedure or not:

    --CHECK WAITTIME SPID DBID  
    select * from sys.sysprocesses  
      
    --check resource and objectid  
    sp_lock  
      
    --check table  
    select * from sys.objects  where object_id = '  '  
      
    --check t_sql  
    select * from sys.dm_exec_requests as p  
    cross apply  sys.dm_exec_sql_text (p.sql_handle) as qt where p.session_id >50  
    

    How to make sure the table lock is released?

    Firstly find the SPID and then commit it or kill it.

    --check open tran   
    DBCC OPENTRAN  
      
    --kill tran  
    Kill SPID  
      
    --commit tran  
    Commit tran   
    

    More information: inf-understanding-and-resolving-sql-server-blocking-problems
    BR,
    Mia


    If the answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

  2. Rachel 1 Reputation point
    2020-11-13T20:52:05.707+00:00

    Thank you for the suggestions!
    I added the "BEGIN TRANSACTION/COMMIT" in the stored procedure. From my application, the table lock was still there, until my application Connection Pool manager closed the connection or I kill the connection manually from Management Studio.
    Plus, I tested it in a standalone java program, the alive DB connection doesn't hold the table lock after running the stored procedure.
    My application is a client-server application, the table lock is not released after running the stored procedure. What's the possible cause? Thank you!


  3. m 4,271 Reputation points
    2020-11-17T01:40:17.417+00:00

    Hi @Rachel ,

    Is the issue solved?
    Is the reply helpful?

    BR,
    Mia


    If the answer is helpful, please click "Accept Answer" and upvote it.


  4. Shashank Singh 6,251 Reputation points
    2020-11-17T05:42:21.697+00:00

    My application is a client-server application, the table lock is not released after running the stored procedure. What's the possible cause? Thank you!

    Your application is not closing the connection, this is the issue which is visible to me. Can you get this checked with app owner.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.