zoukankan      html  css  js  c++  java
  • (转)SQL Server 2005: Get full information about transaction locks

    SQL Server 2005: Get full information about transaction locks

    Sometimes we wish to know what locks are being held by the transaction. Also it would be great to know what SPID owns the transaction, on which objects the locks are being held, what SQL statement caused the locks, etc...

    With the introduction of DMV's in SQL Server 2005 getting this information is quite easy with this query:

    SELECT  L.request_session_id AS SPID, 
            
    DB_NAME(L.resource_database_id) AS DatabaseName,
            O.Name 
    AS LockedObjectName, 
            P.
    object_id AS LockedObjectId, 
            L.resource_type 
    AS LockedResource, 
            L.request_mode 
    AS LockType,
            ST.
    text AS SqlStatementText,        
            ES.login_name 
    AS LoginName,
            ES.
    host_name AS HostName,
            TST.is_user_transaction 
    as IsUserTransaction,
            AT.name 
    as TransactionName,
            CN.auth_scheme 
    as AuthenticationMethod
    FROM    sys.dm_tran_locks L
            
    JOIN sys.partitions P ON P.hobt_id = L.resource_associated_entity_id
            
    JOIN sys.objects O ON O.object_id = P.object_id
            
    JOIN sys.dm_exec_sessions ES ON ES.session_id = L.request_session_id
            
    JOIN sys.dm_tran_session_transactions TST ON ES.session_id = TST.session_id
            
    JOIN sys.dm_tran_active_transactions AT ON TST.transaction_id = AT.transaction_id
            
    JOIN sys.dm_exec_connections CN ON CN.session_id = ES.session_id
            
    CROSS APPLY sys.dm_exec_sql_text(CN.most_recent_sql_handle) AS ST
    WHERE   resource_database_id = db_id()
    ORDER BY L.request_session_id

     Let's look at it one DMV at a time from top to bottom:

    sys.dm_tran_locks:

    Shows us all locks held on all resources for all transaction. We start from this view since we want to get information about locked resources.

    sys.partitions:

    Contains information about partitions in the database. We can join it to the sys.dm_tran_locks on the hobt_id only for resource_type values 'HOBT', 'Page', 'RID'  and 'Key'. With this join we get the object_id of our locked table.

    sys.objects:

    Contains data for all schema scoped database objects. We join it to sys.partitions to get the Name of the locked object which is usually a table name.

    sys.dm_exec_sessions:

    Shows data about all active sessions on SQL server. We join it to sys.dm_tran_locks to get user login information for each held lock.

    sys.dm_tran_session_transactions:

    Shows data for all transactions for a session. By joining in to sys.dm_exec_sessions we get a link to all active transactions in the session.

    sys.dm_tran_active_transactions:

    Shows data for all active transactions in SQL Server. By joining it to sys.dm_tran_session_transactions we only get information for the session we want.

    sys.dm_exec_connections:

    Shows data for all connections in SQL Server. By joining it to sys.dm_exec_sessions we get connection info for our session.

    sys.dm_exec_sql_text:

    Returns SQL statement that is associated with the SQL handle input parameter. By cross applying it to the sys.dm_exec_connections we get the last executed statement for the connection, which in our case is the statement that is holding locks.

    By applying the filter in the where clause you get the answers to questions like:

    - What SQL Statement is causing the lock?

    - Which user has executed the SQL statement that's holding the locks?

    - What objects/tables are being locked?

    - What kinds of locks are being held and on which pages, keys, RID's?

    - etc...

  • 相关阅读:
    JAVA,模拟HTTP登录(略有修改) ----转载自http://zhoujingxian.iteye.com/blog/439738
    java的mysql初探
    java的IO流初探
    java自己写的简单聊天工具SimpleQQ感悟
    java多线程初探
    java事件监听机制2
    JAVA事件监听机制的实现
    iOS (两行代码实现)浮点数保留两位小数且末尾0不展示
    iOS 13.4 & Xcode 11.4 采坑小记(重写系统get)
    Masonry 使用小记(动态宽度依赖)
  • 原文地址:https://www.cnblogs.com/jintan/p/1222157.html
Copyright © 2011-2022 走看看