[sql] view plain copy
---查看現在所有的事務
select '正在運行事務的會話的 ID'=session_id, --session_id與transaction_id的對應關系
'事務的 ID'=transaction_id,
'正在處理事務的會話中的活動請求數'=enlist_count,
'用戶or系統事務'=case is_user_transaction when 1 then '事務由用戶請求啟動'
when 0 then '系統事務'
end,
'本地or分布式事務'= case is_local when 0 then '分布式事務或登記的綁定會話事務'
when 1 then '本地事務'
end,
'分布式事務類型'=case is_enlisted when 0 then '非登記的分布式事務'
when 1 then '登記的分布式事務'
end,
'綁定會話中處於狀態'=case is_enlisted when 0 then '事務在通過綁定會話的會話中處於非活動狀態。'
when 1 then '事務在通過綁定會話的會話中處於活動狀態'
end
from sys.dm_tran_session_transactions --會話中的事務,識別所有打開的事務
where is_user_transaction =1
----活動事務的具體信息
select dt.transaction_id,
dt.name,
dt.transaction_begin_time,
case dt.transaction_type
when 1 then '讀/寫事務'
when 2 then '只讀事務'
when 3 then '系統事務'
when 4 then '分布式事務'
end 'transaction type',
case dt.transaction_state
when 0 then '事務尚未完全初始化'
when 1 then '事務已初始化但尚未啟動'
when 2 then '事務處於活動狀態'
when 3 then '事務已結束。該狀態用於只讀事務'
when 4 then '已對分布式事務啟動提交進程'
when 5 then '事務處於准備就緒狀態且等待解析'
when 6 then '事務已提交'
when 7 then '事務正在被回滾'
when 8 then '事務已回滾'
end 'transaction state',
case dt.dtc_state
when 1 then '活動'
when 2 then '准備就緒'
when 3 then '已提交'
when 4 then '中止'
when 5 then '已恢復'
end dtc_state
from sys.dm_tran_active_transactions dt --活動的事務
where transaction_id = 123
---根據事務ID 和其對應的session_id 找到活動事務對應的執行語句
select dc.session_id,
ds.login_name,
ds.login_time,
dc.connect_time,
dc.client_net_address,
ds.host_name,
ds.program_name,
case ds.status when 'sleeping' then '睡眠 - 當前沒有運行任何請求 '
when 'running' then '正在運行 - 當前正在運行一個或多個請求 '
when 'Dormancy' then '休眠 – 會話因連接池而被重置,並且現在處於登錄前狀態'
when 'Pre-connected' then '預連接 - 會話在資源調控器分類器中'
end as status ,
ds.cpu_time as cpu_time_ms,
ds.memory_usage*8 as memory_kb,
ds.total_elapsed_time as total_elapsed_time_ms,
case ds.transaction_isolation_level when 0 then '未指定'
when 1 then '未提交讀取'
when 2 then '已提交讀取'
when 3 then '可重復'
when 4 then '可序列化'
when 5 then '快照'
end '會話的事務隔離級別',
dt.text
from sys.dm_exec_connections dc --執行連接,最近執行的查詢信息
cross apply sys.dm_exec_sql_text(dc.most_recent_sql_handle) dt
join sys.dm_exec_sessions ds on dc.session_id=ds.session_id
where dc.session_id = 55
-
-
- select '正在運行事務的會話的 ID'=session_id,
- '事務的 ID'=transaction_id,
- '正在處理事務的會話中的活動請求數'=enlist_count,
- '用戶or系統事務'=case is_user_transaction when 1 then '事務由用戶請求啟動'
- when 0 then '系統事務'
- end,
- '本地or分布式事務'= case is_local when 0 then '分布式事務或登記的綁定會話事務'
- when 1 then '本地事務'
- end,
- '分布式事務類型'=case is_enlisted when 0 then '非登記的分布式事務'
- when 1 then '登記的分布式事務'
- end,
- '綁定會話中處於狀態'=case is_enlisted when 0 then '事務在通過綁定會話的會話中處於非活動狀態。'
- when 1 then '事務在通過綁定會話的會話中處於活動狀態'
- end
- from sys.dm_tran_session_transactions
- where is_user_transaction =1
-
-
-
- select dt.transaction_id,
- dt.name,
- dt.transaction_begin_time,
- case dt.transaction_type
- when 1 then '讀/寫事務'
- when 2 then '只讀事務'
- when 3 then '系統事務'
- when 4 then '分布式事務'
- end 'transaction type',
-
- case dt.transaction_state
- when 0 then '事務尚未完全初始化'
- when 1 then '事務已初始化但尚未啟動'
- when 2 then '事務處於活動狀態'
- when 3 then '事務已結束。該狀態用於只讀事務'
- when 4 then '已對分布式事務啟動提交進程'
- when 5 then '事務處於准備就緒狀態且等待解析'
- when 6 then '事務已提交'
- when 7 then '事務正在被回滾'
- when 8 then '事務已回滾'
- end 'transaction state',
- case dt.dtc_state
- when 1 then '活動'
- when 2 then '准備就緒'
- when 3 then '已提交'
- when 4 then '中止'
- when 5 then '已恢復'
- end dtc_state
-
- from sys.dm_tran_active_transactions dt
- where transaction_id = 123
-
-
-
-
- select dc.session_id,
- ds.login_name,
- ds.login_time,
- dc.connect_time,
- dc.client_net_address,
- ds.host_name,
- ds.program_name,
- case ds.status when 'sleeping' then '睡眠 - 當前沒有運行任何請求 '
- when 'running' then '正在運行 - 當前正在運行一個或多個請求 '
- when 'Dormancy' then '休眠 – 會話因連接池而被重置,並且現在處於登錄前狀態'
- when 'Pre-connected' then '預連接 - 會話在資源調控器分類器中'
- end as status ,
- ds.cpu_time as cpu_time_ms,
- ds.memory_usage*8 as memory_kb,
- ds.total_elapsed_time as total_elapsed_time_ms,
- case ds.transaction_isolation_level when 0 then '未指定'
- when 1 then '未提交讀取'
- when 2 then '已提交讀取'
- when 3 then '可重復'
- when 4 then '可序列化'
- when 5 then '快照'
- end '會話的事務隔離級別',
- dt.text
- from sys.dm_exec_connections dc
- cross apply sys.dm_exec_sql_text(dc.most_recent_sql_handle) dt
- join sys.dm_exec_sessions ds on dc.session_id=ds.session_id
- where dc.session_id = 55