Sometimes you need to know who is currently connected to specific database.
While certainly not the only reason, the need for this information arises most often after receiving an error message like this one:
Msg 3702, Level 16, State 3, Line 1
Cannot drop database "AdventureWorks2008R2" because it is currently in use.
This error is informing you that the database can not be dropped right now, because someone is still using it. An error like this will show up for any attempt to execute an action that requires exclusive access to the database.
To proceed with the requested action you have several options. You could for example force the database offline:
[sql] ALTER DATABASE AdventureWorks2008R2 SET OFFLINE WITH ROLLBACK IMMEDIATE;This solution has however several implications that make it unattractive to use. It also does not help you if you really just want to know who is connected to the database right now.
To just get to the information you could run this statement:
[sql] SELECT * FROM master..sysprocesses WHERE dbid = DB_ID('AdventureWorks2008R2');
This was the common recommendation to go with in SQL Server 2000. However, while it still works (even in SQL Server Denali CTP3) the sysprocesses
system table has been deprecated in newer versions of SQL Server.
Instead you could go with the system procedure sp_who
. It is not deprecated (as of Denali CTP 3), but it does not allow you to filter on a specific database, so if you have a lot of concurrent connections it gets quite cumbersome to use. There is also sp_who2
, but it has the same shortcoming and it is also undocumented.
So lets look at the DMVs:
[sql] SELECT * FROM sys.dm_exec_sessions WHERE database_id = DB_ID('AdventureWorks2008R2')
This query gives you very similar information to the first one that queried from sysprocesses
. It's biggest problem is that the database_id
column was introduced in Denali, so you most likely will not be able to use it for a while.
There is a database_id
column in the sys.dm_exec_requests
DMV, but it returns rows only for processes that currently have an active request (statement) running. That means idle connections like an open SSMS query window are not included.
All these methods, besides of their individual problems, have one big shortcoming in common: The information they provide is incomplete.
To drop a database, the executing connection needs to acquire an exclusive lock on the database. This request, like any other lock request will be blocked if there is an incompatible lock held by another connection.
Every connection that does anything in or with a database first requests a lock on that database. Connections that retrieve or alter data in the database will acquire a shared lock on the database. Connections that, like a database drop, require exclusive access to the database require an exclusive lock on it. An exclusive lock — as the name suggests — is not compatible with any other lock, so the requesting process is blocked if any other lock on that database is currently held. SQL Server automatically sets the lock timeout for these statements to a fairly short value, so you will not be blocked forever like you would be in a normal blocking situation with the default lock timeout (infinite).
This means that a single connection, for example by doing a cross database query, can hold a lock on more than one database:
[sql] USE tempdb;
This connection will hold a lock on AdventureWorks2008R2
while the dbid
column in sysprocesses
will point to tempdb
.
So, the dbid
column in sysprocesses
as well as the soon to come database_id
column in sys.dm_exec_sessions
provide the information in which database the next statement will be executed in. The information we are really looking for, we need to get from somewhere else.
This information is provided by the sys.dm_tran_locks
DMV:
This query returns the session id for each session that is currently holding a lock on the AdventureWorks2008R2
database which is exactly the answer to our quest.
If you would like to see a little more information about the established connections, you can use the dbo.DatabaseConnections
function:
dbo.DatabaseConnections
is a table valued function that requires the database id of the database of interest to be passed in. If you pass in a NULL
it will return information about all databases.
For each connection, it provides information about who is connected from where using which software and what type of lock is held on the given database. It also gives information about when the connection was established and the last time for a handful of actions like sending a request or executing a data change. It shows if a transaction is open on the connection and if so, when it was started. It also returns the statement that is (or was) executing together with a little bit of statistical information.
One final note: You cannot use this function to see who is using master
or tempdb
. Because both databases are essential to SQL Servers functioning, SQL Server restricts what you can do to them. Particularly, you cannot drop or restore them. Therefore it is not necessary for SQL Server to track if there is a connection the still needs these databases around. To save the overhead of locking, connections do not need to acquire a database lock on either of the two databases. That in turn means, that the above function will not return any information about connections that are using only one of these two databases.