SQL Server查询事务

SQL Server查询事务

2018年07月08日 19:25:00 weixin_33989058 阅读数 108

无论是有意无意,如果事务在数据库中保持打开,则它会阻塞其他进程对修改后的数据进行操作(ACID特性)。同样,长时间运行的事务也可能导致日志填满。长时间运行的事务可使事务日志从包含事务的第一条日志记录的虚拟日志文件开始,便一直保持活动状态,以打开的事务会导致日志变多(甚至达到物理限制),直到事务被提交或回滚。

(1)通过DBCC OPENTRAN查询事务

1.1 DBCC OPENTRAN简介

通过DBCC OPENTRAN语句,您可以标识该事务所有者的用户 ID,因此可以隐性地跟踪该事务的源以得到更加有序的终止(将其提交而非回滚)。该命令也返回在指定数据库内存在最早的活动事务和最早的分布式和非分布式复制事务。如果没有活动事务,则显示信息性消息,而不返回会话级数据。
  DBCC OPENTRAN对于孤立连接(在数据库中是打开的,但与应用程序或客户端已经断开的连接)是非常有用的,并能帮助我们找出遗漏了COMMIT或ROLLBACK的事务。

1.2 DBCC OPENTRAN使用示例

DBCC OPENTRAN语法


 
  1. DBCC OPENTRAN

  2. [

  3. ( [ database_name | database_id | 0 ] ) ]

  4. { [ WITH TABLERESULTS ]

  5. [ , [ NO_INFOMSGS ] ]

  6. }

  7. ]

  • database_name | database_id| 0:Is the name or ID of the database for which to display the oldest transaction information. If not specified, or if 0 is specified, the current database is used.
  • TABLERESULTS:Specifies the results in a tabular format that can be loaded into a table. Use this option to create a table of results that can be inserted into a table for comparisons.
  • NO_INFOMSGS:Suppresses all informational messages.

使用示例

A. Returning the oldest active transaction


 
  1. CREATE TABLE T1(Col1 int, Col2 char(3));

  2. GO

  3. BEGIN TRAN

  4. INSERT INTO T1 VALUES (101, 'abc');

  5. GO

  6. DBCC OPENTRAN;

  7. ROLLBACK TRAN;

  8. GO

  9. DROP TABLE T1;

  10. GO

【执行结果】
  结果显示了最早活动日志的相关信息,包括服务器进程ID(SPID)、用户ID(UID)、和事务的开始时间,关键是SPID和Start Time。

4444657-07c7a86979617ff2.png

B. Specifying the WITH TABLERESULTS option
  The following example loads the results of the DBCC OPENTRAN command into a temporary table.


 
  1. -- Create the temporary table to accept the results.

  2. CREATE TABLE #OpenTranStatus (

  3. ActiveTransaction varchar(25),

  4. Details sql_variant

  5. );

  6. -- Execute the command, putting the results in the table.

  7. INSERT INTO #OpenTranStatus

  8. EXEC ('DBCC OPENTRAN WITH TABLERESULTS, NO_INFOMSGS');

  9.  
  10. -- Display the results.

  11. SELECT * FROM #OpenTranStatus;

  12. GO

(2)通过DMV(动态管理视图)查询事务

1、 sys.dm_tran_session_transactions:提供视图相关的信息,并包含了特定会话的信息。

4444657-e223c40bd15f831e.png

2、 sys.dm_tran_active_transactions:返回实例级别上,所以正在活动的事务信息。
3、 sys.dm_tran_database_transactions:返回数据库级别上的事务信息。

对于长时间运行的事务,最需要注意的列包括:第一条日志记录的时间 、事务的当前状态 (database_transaction_state) 和事务日志中开始记录的日志序列号 (LSN) 。

实例、数据库和会话

  • 数据库是属于某个实例的,就是说一个实例包含多个数据库。
  • 会话 :用户通过用户进程与SQL Server实例建立的连接[此处连接主要指用户和数据库间的联系 ]。例如,当用户启动SQL Server时必须提供有效的用户名和密码,之后SQL Server为此用户建立一个会话。从用户开始连接到用户断开连接(或退出数据库应用程序)期间,会话一直持续。

示例


 
  1. SET Transaction isolation level serializable

  2. BEGIN TRAN

  3. select * from T_Product

  4. Insert into T_Product select 'OATest' union all select 'OAPlay'

这是一个未提交的事务,在另一个查询窗口执行如下:


 
  1. select session_id,transaction_id,is_user_transaction,is_local

  2. from sys.dm_tran_session_transactions

  3. where is_user_transaction=1 --

执行结果:


 
  1. session_id transaction_id is_user_transaction is_local

  2. 54 489743 1 1

返回会话ID(session_id)后,可以通过sys.dm_exec_connections和sys.dm_exec_sql_text来挖掘最近执行的查询的详细信息:


 
  1. select s.text from sys.dm_exec_connections c

  2. cross apply sys.dm_exec_sql_text(c.most_recent_sql_Handle) s

  3. where session_id=54

这个查询返回最后执行的语句。
  因为也从sys.dm_tran_session_transactions的第一个查询中得知事务ID,所以可以使用sys.dm_tran_active_transactions来了解更多事务本身的内容。


 
  1. select transaction_begin_time,

  2. case transaction_type

  3. when 1 then 'Read/Write transaction'

  4. when 2 then 'Read-Only transaction'

  5. when 3 then 'System transaction'

  6. when 4 then 'Distributed transaction'

  7. end tran_Type,

  8. case transaction_state

  9. when 0 then 'not been comoletely initaialiaed yet'

  10. when 1 then 'initaialiaed but ha notstarted'

  11. when 2 then 'active'

  12. when 3 then 'ended (read-only transaction)'

  13. when 4 then 'commit initiated for distributed transaction'

  14. when 5 then 'transaction prepared and waiting resolution'

  15. when 6 then 'commited'

  16. when 7 then 'being rolled back'

  17. when 0 then 'been rolled back'

  18. end transaction_state

  19. from

  20. sys.dm_tran_active_transactions

  21. where transaction_ID=455520

执行结果:


 
  1. transaction_begin_time tran_Type transaction_state

  2. 2010-12-24 14:05:29.170 Read/Write transaction active

原文地址:https://www.cnblogs.com/grj001/p/12224969.html