简体   繁体   English

如何检查 SQL Server 2014 中的活动事务?

[英]How to check active transactions in SQL Server 2014?

我正在使用 SQL Server 2014 并且想知道如何检查我的活动事务?

  1. Query with sys.sysprocesses 使用sys.sysprocesses查询

     SELECT * FROM sys.sysprocesses WHERE open_tran = 1 
  2. DBCC OPENTRAN : helps to identify active transactions that may be preventing log truncation. DBCC OPENTRAN :帮助识别可能阻止日志截断的活动事务。 DBCC OPENTRAN displays information about the oldest active transaction and the oldest distributed and nondistributed replicated transactions, if any, within the transaction log of the specified database. DBCC OPENTRAN在指定数据库的事务日志中显示有关最早的活动事务以及最早的分布式和非分布式复制事务(如果有)的信息。 Results are displayed only if there is an active transaction that exists in the log or if the database contains replication information. 仅当日志中存在活动事务或数据库包含复制信息时,才会显示结果。 An informational message is displayed if there are no active transactions in the log. 如果日志中没有活动事务,则显示信息性消息。

  3. sys.dm_tran_active_transactions

Returns information about transactions for the instance of SQL Server. 返回有关SQL Server实例的事务的信息。 Syntax 句法

在此输入图像描述

Wondering about Transaction ? 想知道交易?

A transaction is a single unit of work. 交易是一个单一的工作单位。 If a transaction is successful, all of the data modifications made during the transaction are committed and become a permanent part of the database. 如果事务成功,则在事务期间进行的所有数据修改都将被提交并成为数据库的永久部分。

Find more at docs 在docs上查找更多内容

Translation the 3. query that Tharif comments. 翻译3.查询Tharif评论。

select transaction_id, name, transaction_begin_time
 ,case transaction_type 
    when 1 then '1 = Read/write transaction'
    when 2 then '2 = Read-only transaction'
    when 3 then '3 = System transaction'
    when 4 then '4 = Distributed transaction'
end as transaction_type 
,case transaction_state 
    when 0 then '0 = The transaction has not been completely initialized yet'
    when 1 then '1 = The transaction has been initialized but has not started'
    when 2 then '2 = The transaction is active'
    when 3 then '3 = The transaction has ended. This is used for read-only transactions'
    when 4 then '4 = The commit process has been initiated on the distributed transaction'
    when 5 then '5 = The transaction is in a prepared state and waiting resolution'
    when 6 then '6 = The transaction has been committed'
    when 7 then '7 = The transaction is being rolled back'
    when 8 then '8 = The transaction has been rolled back'
end as transaction_state
,case dtc_state 
    when 1 then '1 = ACTIVE'
    when 2 then '2 = PREPARED'
    when 3 then '3 = COMMITTED'
    when 4 then '4 = ABORTED'
    when 5 then '5 = RECOVERED'
end as dtc_state 
,transaction_status, transaction_status2,dtc_status, dtc_isolation_level, filestream_transaction_id
from sys.dm_tran_active_transactions

If you want to know more details about active sessions like session ID, Host Name,Login Name,Transaction ID,Transaction Name,Trnasaction Begin Time,Databse ID,Database Name you can use below sql query 如果您想了解有关活动会话的更多详细信息,例如会话ID,主机名,登录名,交易ID,交易名称,Trnasaction开始时间,数据库ID,数据库名称,您可以在sql查询下使用

SELECT
trans.session_id AS [SESSION ID],
ESes.host_name AS [HOST NAME],login_name AS [Login NAME],
trans.transaction_id AS [TRANSACTION ID],
tas.name AS [TRANSACTION NAME],tas.transaction_begin_time AS [TRANSACTION 
BEGIN TIME],
tds.database_id AS [DATABASE ID],DBs.name AS [DATABASE NAME]
FROM sys.dm_tran_active_transactions tas
JOIN sys.dm_tran_session_transactions trans
ON (trans.transaction_id=tas.transaction_id)
LEFT OUTER JOIN sys.dm_tran_database_transactions tds
ON (tas.transaction_id = tds.transaction_id )
LEFT OUTER JOIN sys.databases AS DBs
ON tds.database_id = DBs.database_id
LEFT OUTER JOIN sys.dm_exec_sessions AS ESes
ON trans.session_id = ESes.session_id
WHERE ESes.session_id IS NOT NULL

and you will get the result something like 你会得到类似的结果 在此输入图像描述

或使用DBCC命令

DBCC OPENTRAN

The Most Usefull method is;最有用的方法是;

DBCC opentran()

When You check, you will get below message;当您检查时,您将收到以下消息;

Oldest active transaction: SPID (server process ID): 190 UID (user ID) : -1 Name : implicit_transaction LSN : (500549:37333:1) Start time : Dec 4 2021 10:36:21:673AM最旧的活动事务:SPID(服务器进程 ID):190 UID(用户 ID):-1 名称:implicit_transaction LSN:(500549:37333:1) 开始时间:2021 年 12 月 4 日 10:36:21:673AM

And if you run DBCC opentran several times and you always get same server process ID then system then A transaction is stuck in the database.如果您多次运行 DBCC opentran 并且您总是获得与系统相同的服务器进程 ID,那么事务就会卡在数据库中。

Therefore, it is necessary to first look at the SPID detail with the code below and then kill that SPID process.因此,有必要先用下面的代码查看 SPID 的详细信息,然后杀死该 SPID 进程。

exec sp_who2 190
exec sp_lock 190


KILL 190

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM