我想检查SQL Server中的每个spid /会话隔离级别。我的问题特定于“读取提交的快照”

时间:2019-01-08 15:08:18

标签: sql-server-2014

如何检查当前所有正在运行的会话的隔离级别。我们在数据库级别设置的是READ COMMITTED SNAPSHOT。我想确保每个会话都在使用它,以确保在创建到SQL Server的会话连接时不会通过应用程序更改某些内容

我已经在下面的查询中尝试过,但是它只显示“ READ COMMITTED”。

SELECT transaction_sequence_num,
       commit_sequence_num,
       is_snapshot,
       t.session_id,
       first_snapshot_sequence_num,
       max_version_chain_traversed,
       elapsed_time_seconds,
       host_name,
       login_name,
       CASE transaction_isolation_level
           WHEN '0' THEN
               'Unspecified'
           WHEN '1' THEN
               'ReadUncomitted'
           WHEN '2' THEN
               'ReadCommitted'
           WHEN '3' THEN
               'Repeatable'
           WHEN '4' THEN
               'Serializable'
           WHEN '5' THEN
               'Snapshot'
       END AS transaction_isolation_level
FROM sys.dm_tran_active_snapshot_database_transactions t
    JOIN sys.dm_exec_sessions s
        ON t.session_id = s.session_id;

2 个答案:

答案 0 :(得分:0)

I think this may work
SELECT transaction_sequence_num,
       commit_sequence_num,
       is_snapshot,
       t.session_id,
       first_snapshot_sequence_num,
       max_version_chain_traversed,
       elapsed_time_seconds,
       host_name,
       login_name,
       CASE transaction_isolation_level
           WHEN '0' THEN
               'Unspecified'
           WHEN '1' THEN
               'ReadUncomitted'
           WHEN '2' THEN
               CASE
                   WHEN
                   (
                       SELECT [is_read_committed_snapshot_on]
                       FROM sys.databases
                       WHERE [database_id] = s.[database_id]
                   ) = 1 THEN
                       'ReadCommittedSnapShot'
                   ELSE
                       'ReadCommitted'
               END
           WHEN '3' THEN
               'Repeatable'
           WHEN '4' THEN
               'Serializable'
           WHEN '5' THEN
               'Snapshot'
       END AS transaction_isolation_level
FROM sys.dm_tran_active_snapshot_database_transactions t
    JOIN sys.dm_exec_sessions s
        ON t.session_id = s.session_id;

答案 1 :(得分:0)

由于您发布的查询内部联接到dm_tran_active_snapshot_database_transactions,因此仅返回使用行版本控制的会话。要使用“读取已提交快照隔离级别”(RSCI)返回会话,并使用其他隔离级别返回会话,请加入dm_tran_active_snapshot_database_transactions,并检查是否存在不匹配项(“空连接”列)。我不确定您是否需要dm_tran_active_snapshot_database_transactions中列出的所有列,因此下面是一个精简版本,是否可以根据需要添加任何列。 total_elapsed_time中的dm_exec_sessions以毫秒为单位,而elapsed_time_seconds中的dm_tran_active_snapshot_database_transactions以秒为单位,此示例以秒为单位返回结果,而不管会话来自哪个DMV。我还建议始终对列使用别名,尤其是在涉及联接时。

SELECT 
S.session_id,
S.host_name,
S.login_name,
--divide milliseconds in total_elapsed_time by 1000 
--to get time in seconds if session not using Read Committed SnapShot
COALESCE(T.elapsed_time_seconds, (S.total_elapsed_time / 1000.0)) as TimeElapsedInSeconds,
S.transaction_isolation_level,
CASE WHEN T.session_id is not null then 'ReadCommittedSnapShot' ELSE
CASE S.transaction_isolation_level
WHEN 0 THEN 'Unspecified'
WHEN 1 THEN 'ReadUncomitted'
WHEN 2 THEN 'ReadCommitted'
WHEN 3 THEN 'Repeatable'
WHEN 4 THEN 'Serializable'
WHEN 5 THEN 'Snapshot' END
END as transaction_isolation_level
FROM SYS.DM_EXEC_SESSIONS S
LEFT JOIN SYS.DM_TRAN_ACTIVE_SNAPSHOT_DATABASE_TRANSACTIONS T on S.session_id = T.session_id