(session ID -2 is not valid) Dealing with Negative Session ids in Sql Server

 

SQL server being a complex mechanism of handling processes throughout their life cycle often gives out interesting anomalies,

One such was encountered today on one of my production servers, there was a huge blocking on chain of processes and at the root of this chain was a session with the weirdest session id 

The session id literally was " -2 "  ,  it is hard to make sense out of it as SQL server always works with positive spids. Research on this is itself a tedious job. After the R&D phase it was somewhat easier to diagnose the problem.

The root cause refers to a phase of distributed transaction, A distributed transaction is referred when a process is extended over more than one servers i.e Process A of server 1 needs data to be manipulated on Server 2, so SQL server uses MSDTC (linked server) service to carry this process from one server to another.

Going back to our issue, now

According to msdn and other online resources, the session id -2 means that the MSDTC service no longer knows what to do for that process\transaction, it is " in-doubt ", it can no longer refer it to sql server. Often termed as Orphaned transactions , these have unknown state.

Possible causes can be Network issue, MSDTC service crashing or sudden restart of the same service while there was a heavy dml transaction in execution.

Even a remote query timeout in this chain of MSDTC transaction can cause the service to fail the transaction, The Failed transactions rollback at a speed depending on the amount of changes it made. 

Killing such process is the solution, Yes you might opt to hard bounce the server in order to get rid of any pending transactions,  but hard bounce with a scenario where possibly a heavy transaction is in rollback or commit will only cause difficulties in recovering the database once it comes online. 

Another is to check in "Transaction Statistics" of  "Local DTC" in "Component Services" , it will actually show the in-doubt transactions and then give a option to resolve (commit,abort,forget), if it doesn't then go on to kill 

A session with negative spid cannot be killed using our common kill command, if tried it will give you an error such as 

Msg 6101, Level 16, State 1, Line 1

Process ID -2 is not a valid process ID. Choose a number between 1 and 2048


need to follow the below steps to kill the process 

1. select req_transactionUOW from master..syslockinfo where req_spid = -2

2. KILL <req_transactionUOW >



Comments

Popular posts from this blog

SQL Server’s STATISTICS IO

ORA says NO DATA FOUND