在SQL中跟踪.NET事务

时间:2017-08-30 02:21:23

标签: sql .net transactions

我定义了这样的事务(VB.NET)

Dim objTransaction As New Transaction(IsolationLevel.ReadCommitted, "OpenTransaction")

执行这些流程,然后使用

关闭它
 objTransaction.Commit()
 objTransaction.Dispose()

objTransaction.Rollback()

如果有错误。

现在如果交易是开放的,我怎样才能找到" OpenTransaction"在SQL中?

我做了

SELECT * FROM sys.dm_tran_active_transactions 

并且它给出了 - name =" user_transaction",而不是" OpenTransaction"。如何在SQL中找到我给它的名字?

(我有几个用户在网页上做了不同的事情,我发现在SQL中有一个长时间运行的开放事务,但是无法找到我在代码中给出的名称。)

1 个答案:

答案 0 :(得分:0)

您可以使用SQL事件探查器来实现此目的。您可以在SQL管理工作室中启动它,然后设置您的过滤器并启动Trace

检查this链接以获取更多信息

<强>更新

您也可以使用此查询来监控交易:

USE [master]
GO
CREATE PROCEDURE [dbo].[sp_who3]

AS
BEGIN

SET
   TRANSACTION isolation level READ uncommitted;
SELECT
   SPID = er.session_id,
   BlkBy = 
   CASE
      WHEN
         lead_blocker = 1 
      THEN
         - 1 
      ELSE
         er.blocking_session_id 
   END
, ElapsedMS = er.total_elapsed_time , CPU = er.cpu_time , IOReads = er.logical_reads + er.reads , IOWrites = er.writes , Executions = ec.execution_count , CommandType = er.command , LastWaitType = er.last_wait_type , ObjectName = Object_schema_name(qt.objectid, dbid) + '.' + Object_name(qt.objectid, qt.dbid) , SQLStatement = Substring ( qt.text, er.statement_start_offset / 2, 
   CASE
      WHEN
         (
            CASE
               WHEN
                  er.statement_end_offset = - 1 
               THEN
                  Len(CONVERT(NVARCHAR(max), qt.text)) * 2 
               ELSE
                  er.statement_end_offset 
            END
            - er.statement_start_offset / 2 
         )
         < 0 
      THEN
         0 
      ELSE
         CASE
            WHEN
               er.statement_end_offset = - 1 
            THEN
               Len(CONVERT(NVARCHAR(max), qt.text)) * 2 
            ELSE
               er.statement_end_offset 
         END
         - er.statement_start_offset / 2 
   END
) , STATUS = ses.status , [Login] = ses.login_name , Host = ses.host_name , DBName = Db_name(er.database_id) , StartTime = er.start_time , Protocol = con.net_transport , transaction_isolation = 
   CASE
      ses.transaction_isolation_level 
      WHEN
         0 
      THEN
         'Unspecified' 
      WHEN
         1 
      THEN
         'Read Uncommitted' 
      WHEN
         2 
      THEN
         'Read Committed' 
      WHEN
         3 
      THEN
         'Repeatable' 
      WHEN
         4 
      THEN
         'Serializable' 
      WHEN
         5 
      THEN
         'Snapshot' 
   END
, ConnectionWrites = con.num_writes , ConnectionReads = con.num_reads , ClientAddress = con.client_net_address , Authentication = con.auth_scheme , DatetimeSnapshot = Getdate() , plan_handle = er.plan_handle 
FROM
   sys.dm_exec_requests er 
   LEFT JOIN
      sys.dm_exec_sessions ses 
      ON ses.session_id = er.session_id 
   LEFT JOIN
      sys.dm_exec_connections con 
      ON con.session_id = ses.session_id OUTER apply sys.Dm_exec_sql_text(er.sql_handle) AS qt OUTER apply ( 
      SELECT
         execution_count = Max(cp.usecounts) 
      FROM
         sys.dm_exec_cached_plans cp 
      WHERE
         cp.plan_handle = er.plan_handle ) ec OUTER apply ( 
         SELECT
            lead_blocker = 1 
         FROM
            master.dbo.sysprocesses sp 
         WHERE
            sp.spid IN 
            (
               SELECT
                  blocked 
               FROM
                  master.dbo.sysprocesses WITH (nolock) 
               WHERE
                  blocked != 0
            )
            AND sp.blocked = 0 
            AND sp.spid = er.session_id ) lb 
         WHERE
            er.sql_handle IS NOT NULL 
            AND er.session_id != @@SPID 
         ORDER BY
            CASE
               WHEN
                  lb.lead_blocker = 1 
               THEN
                  - 1 * 1000 
               ELSE
                  - er.blocking_session_id 
            END
, er.blocking_session_id DESC, er.logical_reads + er.reads DESC, er.session_id;
END

并创建一个SP。你可以使用

  

EXEC sp_who3

它将为您提供用户拥有的所有正在运行的交易,您也可以根据需要对其进行编辑。