SQLServer 2008中通過DBCC OPENTRAN和會話查詢事務
要找到最早的活動事務,可以使用DBCC OPENTRAN命令。詳細用法見MSDN:http://msdn.microsoft.com/zh-cn/library/ms182792.aspx
給出一個示例:
CREATE TABLE T_Product(PKID int, PName Nvarchar(50));
GO
BEGIN TRAN
INSERT INTO T_Product VALUES (101, '嫦娥四號');
GO
DBCC OPENTRAN;
ROLLBACK TRAN;
GO
DROP TABLE T_Product;
GO
執(zhí)行結果:
/*
(1 row(s) affected)
數(shù)據(jù)庫 'Testdb' 的事務信息。
最早的活動事務:
SPID (服務器進程 ID): 54
UID (用戶 ID): -1
名稱 : user_transaction
LSN : (295:6687:1)
開始時間 : 12 24 2010 2:50:15:607PM
SID : 0x0105000000000005150000007fe010d31cba1ab1566ac5dff4010000
DBCC 執(zhí)行完畢。如果 DBCC 輸出了錯誤信息,請與系統(tǒng)管理員聯(lián)系。
*/
結果顯示了最早活動日志的相關信息,包括服務器進程ID、用戶ID、和事務的開始時間。關鍵是SPID和Start Time。
擁有這些信息后,可以使用動態(tài)管理視圖(DMV)來檢驗正在執(zhí)行的T-SQL,以及在必要時關閉這個過程
DBCC OPENTRAN對于孤立連接(在數(shù)據(jù)庫中是打開的,但與應用程序或客戶端已經(jīng)斷開的連接)是非常有用的,并能幫助我們找出遺漏了COMMIT或ROLLBACK的事務。該命令也返回在指定數(shù)據(jù)庫內(nèi)存在最早的活動事務和最早的分布式和非分布式復制事務。如果沒有活動事務,則顯示信息性消息,而不返回會話級數(shù)據(jù)。
我們看一個實例:
SET Transaction isolation level serializable
BEGIN TRAN
select * from T_Product
Insert into T_Product
select 'OATest' union all
select 'OAPlay'
這是一個未提交的事務,在另一個查詢窗口執(zhí)行如下:
select session_id,transaction_id,is_user_transaction,is_local
from sys.dm_tran_session_transactions
where is_user_transaction=1
執(zhí)行結果:
/*返回結果
session_id transaction_id is_user_transaction is_local
54 489743 1 1
*/
返回會話ID后,可以通過sys.dm_exec_connections和sys.dm_exec_sql_text來挖掘最近執(zhí)行的查詢的詳細信息。
select s.text from sys.dm_exec_connections c
cross apply sys.dm_exec_sql_text(c.most_recent_sql_Handle) s
where session_id=54
這個查詢返回最后執(zhí)行的語句。也可以使用sys.dm_exec_requests。
因為也從sys.dm_tran_session_transactions的第一個查詢中得知事務ID,所以可以使用sys.dm_tran_active_transactions來了解更多事務本身的內(nèi)容
select transaction_begin_time,
case transaction_type
when 1 then 'Read/Write transaction'
when 2 then 'Read-Only transaction'
when 3 then 'System transaction'
when 4 then 'Distributed transaction'
end tran_Type,
case transaction_state
when 0 then 'not been comoletely initaialiaed yet'
when 1 then 'initaialiaed but ha notstarted'
when 2 then 'active'
when 3 then 'ended (read-only transaction)'
when 4 then 'commit initiated for distributed transaction'
when 5 then 'transaction prepared and waiting resolution'
when 6 then 'commited'
when 7 then 'being rolled back'
when 0 then 'been rolled back'
end transaction_state
from
sys.dm_tran_active_transactions
where transaction_ID=455520
/*結果:
transaction_begin_time tran_Type transaction_state
2010-12-24 14:05:29.170 Read/Write transaction active
*/
小結:這里演示了使用DMV 排除故障和調(diào)查長時間的活動事務的一般技巧?;静襟E如下:
1、查詢sys.dm_tran_session_transactions獲取會話ID和事務ID之間的映射。
2、查詢sys.dm_exec_connections和sys.dm_exec_sql_text查找會話最新執(zhí)行的命令(most_recent_sql_Handle列)
3、最后,查詢sys.dm_tran_active_transactions確定事務被打開了多少時間、事務的類型和事務的狀態(tài)。
使用這個技巧可以回到應用程序去查明調(diào)用的被拋棄的事務(打開但從未提交)以及那些運行時間太長或?qū)τ趹贸绦騺碚f是不必要的不恰當事務。
相關文章
SQLServer 2005系統(tǒng)配置要求官方說明
SQLServer 2005系統(tǒng)配置要求官方說明,需要安裝sql2005的朋友需要了解下。2009-08-08
SQLServer2005與SQLServer2008數(shù)據(jù)庫同步圖文教程
要實現(xiàn)SQLServer2005與2005的數(shù)據(jù)庫同步的話,直接用鏡像就可以實現(xiàn)。但是如果同步 SQLServer2008的話,2005的實例是連接不上08的。低版本的無法連接高版本的。所以我們可以通過復制的方式,也就是所謂的訂閱發(fā)布的方法來實現(xiàn)兩個不同版本數(shù)據(jù)庫的數(shù)據(jù)同步。2011-09-09
Win2008中SqlServer2008 無法打開錯誤日志文件導致無法啟動的解決方法
今天早上一個客戶的SqlServer 2008的服務器應為重新修改配置導致網(wǎng)站打不開,提示initerrlog: 無法打開錯誤日志文件2011-12-12
MS-sql 2005拒絕了對對象 ''xxx'' (數(shù)據(jù)庫 ''xxx'',架構 ''dbo'')的 SELECT 權
訪問了提示“MS-sql 2005拒絕了對對象 'xxx' (數(shù)據(jù)庫 'xxx',架構 'dbo')的 SELECT 權限”的錯誤2008-05-05
SqlServer2005中使用row_number()在一個查詢中刪除重復記錄的方法
在SqlServer2005中,提供了一個row_number()的函數(shù),我們經(jīng)常用它做DataBase數(shù)據(jù)分頁.2011-10-10

