Solved

Sql server lock cursor

Posted on 2016-10-25
13
58 Views
Last Modified: 2016-11-21
good evening

I'm blocking problem in sqlserver caused by cursor.

The head block I see is a fatch_api_cursor, the problem is that when I try to see the SQL code inside the cursor, it shows nothing and the status is like sleeping.

any idea how I can detect what is inside the cursor?

I've tried to dmv sys.dm_exec cursors but does not provide any information.

I have to kill the process to release the locks.

thank's
0
Comment
Question by:Alex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41860055
Try...10 should be your sessionid

SELECT c.session_id, c.properties, c.creation_time, c.is_open
, t.text
FROM sys.dm_exec_cursors (10) c
CROSS APPLY sys.dm_exec_sql_text (c.sql_handle) t

Open in new window

0
 

Author Comment

by:Alex
ID: 41860442
hello,

Unfortunately I had tried it, but does not provide any information when the status of the process is sleeping.

It shows information only when the status of the process is runnable which is not my case.

Any other ideas?
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41860450
So if you are sure nothing is happening in that session, why can't you go head and KILL the session itself.
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 

Author Comment

by:Alex
ID: 41860467
I have a lock chain, when I check the head block and see what is running, get running fetch_api_cursor ...

When you check the DMV sys.dm_exec_cursors does not provide any information for this process.

Some processes that are sometimes head block to bring the information below:

IF @@ TRANCOUNT> 0 COMMIT TRAN

But the vast majority, does not provide any information.
0
 
LVL 12

Expert Comment

by:Dimitris
ID: 41862162
could it be that the cursor is in an endless loop? (@@FETCH_STATUS issue)

if you see the process running it means that it's waiting for something
Try to change the CURSOR to LOCAL (DECLARE rst CURSOR LOCAL)
are you closing the cursor (CLOSE rst and DEALLOCATE rst )?
and if you are not updating anything with the cursor and you are just reading data make it also a FAST_FORWARD cursor (it means forward only, readonly)

If you could provide some code lines it would be easier
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41871742
Hi Alex, Any luck with this?

Regards,
Pawan
0
 

Author Comment

by:Alex
ID: 41882842
Hello,

Unfortunately it still could not detect which sql statement is running inside the cursor, even using the sys.dm_exec_cursors dmv.

The process status that is as headblock is sleeping
0
 

Author Comment

by:Alex
ID: 41883078
Based on the information in this link, it is very similar to the scenario that is occurring in the environment.

https://blogs.msdn.microsoft.com/psssql/2008/04/21/how-it-works-what-is-a-sleepinga-awaiting-command-session/

Can the set xact_abort option help?
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41883263
XACT_ABORT informs SQL Server to rollback the entire transaction and abort the batch when a run-time error occurs.

Actually the link you gave is not working. :)

Try XACT_ABORT it works. Also if you want the hard way we can use KILL sessionId to kill the session. Although it is not recommended.

Hope it helps !
0
 

Author Comment

by:Alex
ID: 41883499
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41883520
0
 

Author Comment

by:Alex
ID: 41883731
I have already referred to the development team to approve.

Thank you
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41883760
Great ! Please inform me if you get reply from the development team.

Thank you !
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
convert null in sql server 12 57
Amazon RDS migrate to SQL Server 3 33
query output (script) from a stored procedure 4 38
find SQL job run average duration 24 55
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question