Transactions: Who, What and Where


January 20, 2014 by Kenneth Fisher

Recently we had a scenario where we had a handful of queries being blocked. Nothing unusual there but when I looked into sys.dm_exec_requests I could see all of the blocked requests, but could not find a request with a session_id matching the blocking_session_id. The session showed up in sys.dm_exec_sessions but it was “sleeping” and hadn’t performed a request in hours. So what was going on?

Well unfortunately sys.dm_exec_requests only pulls currently executing requests. A session that has an open transaction but isn’t actively doing anything isn’t considered an “executing” request and won’t show up in sys.dm_exec_requests. In order to get a list of sessions with an open transaction you can run a query off sys.dm_tran_session_transactions or if you are running SQL 2012 or higher sys.dm_exec_sessions has a new column open_transaction_count. However we need do something about our blocking transaction and as I see it we have a couple of options.

Easiest is to find and talk to the user. Sys.dm_exec_sessions has the login_name, nt_domain, nt_user_name to help you identify the user, but if they are using a generic SQL login then that won’t help much. Next we can look at host_name to find the users machine and program_name to tell what program they are connecting from. (Frequently it helps to tell the user what program is the problem if they are using Excel, Access, and SSMS to connect to the instance.) And if you want to you can look at sys.dm_exec_connections and get the IP(client_net_address) of the connecting machine.

But let’s say that it’s the middle of the night and now we need to decide if we are going to kill the process, wake up the user (if we have their number), or just let it run till morning. In order to make that decision it would help to know what exactly they are doing. If we had a row in sys.dm_exec_requests we could use sys.dm_exec_sql_text to get the actual query they are running. But as we said before there is no row in sys.dm_exec_requests. So now we again have a couple of options. Simplest is to look at sys.dm_exec_connections and use the most_recent_sql_handle column with sys.dm_exec_sql_text to get the last query run by the connection. Unfortunately it does not give us all of the SQL statements within the transaction. It only returns the set of statements in the last batch executed within the transaction. I’ll post a proof soon. So that may not give us enough information. If so we can go the somewhat more complicated route and take a look at the locks held by the session.

We can tie sys.dm_exec_sessions to sys.dm_tran_session_transactions to get a list of the transactions tied to the session.

FROM sys.dm_exec_sessions sessions
JOIN sys.dm_tran_session_transactions trans
	ON sessions.session_id = trans.session_id

On the other hand sys.dm_tran_locks has session_id also and we can get a lot of additional information that will be very helpful.

SELECT request_session_id AS session_id,
	request_owner_id AS transaction_id,
	DB_NAME(resource_database_id) AS DatabaseName,
			resource_database_id) AS SchemaName,
			resource_database_id) AS ObjectName,
	request_mode, request_type, request_status,
	COUNT_BIG(1) AS lock_count
FROM sys.dm_tran_locks
WHERE resource_type = 'OBJECT'
GROUP BY request_session_id, request_owner_id,
	resource_database_id, resource_associated_entity_id,
	request_mode, request_type, request_status

I did run into a bit of a problem here. Sometimes (and I’m not sure when or why) I got blocked when trying to use OBJECT_NAME and OBJECT_SCHEMA_NAME in this query. You could join to sys.objects and sys.schemas instead but only one database at a time. If you want to use this query as it stands you can try running it, and if it gets blocked kill it. Then query for just the resource_database_id to figure out which database you need, then go there and join to sys.objects and sys.schemas. Cumbersome but I don’t know a better way I’m afraid.

Now I’m only looking at object locks in this query but there are a number of other types that you may want to look at (DATABASE, FILE, PAGE etc). In general I’ve found the OBJECT locks to be the most useful though. By combining object_name, request_mode, request_type and request_status we can get a fairly good idea of what the individual is doing. If they are only doing a series of selects there is no problem you should be fairly safe in killing the connection. If they are doing updates, inserts, deletes etc you can take the # of locks, your knowledge of the tables and their use and make an informed decision.

Transactions are a big subject which I’m going to explore over several posts. I am by no means going to cover the subject exhaustively but if you have any subjects you would like me to cover or think I’ve missed something feel free to comment or email me.

One thought on “Transactions: Who, What and Where

  1. […] is not equal to 0 to find blocking sessions. I wrote about this in more detail in my post Transactions: Who, What and Where and my SQL Server Central article Kill SPID (SQL […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 1,597 other followers

Follow me on Twitter

ToadWorld Pro of the Month November 2013
%d bloggers like this: