Solved

View lock information using Activity Monitor

Posted on 2010-08-13
10
910 Views
Last Modified: 2012-05-10
I expertr. i need to know about
View lock information using Activity Monitor
Use the dynamic management view to view lock info
an url please or article,
0
Comment
Question by:enrique_aeo
  • 5
  • 4
10 Comments
 
LVL 13

Accepted Solution

by:
dwkor earned 500 total points
ID: 33434913

/*
Shows current locks. Even if it works across all database, ObjectName 
populates for current database only. Could be modified with dynamic SQL if needed

Be careful with Query text for LOCKS with GRANT status. This represents currently active
request for this specific session id which could be different than query which produced locks
It also could be NULL if there are no active requests for this session
*/

select
	TL1.resource_type
	,DB_NAME(TL1.resource_database_id) as [DB Name]
	,CASE TL1.resource_type
		WHEN 'OBJECT' THEN OBJECT_NAME(TL1.resource_associated_entity_id, TL1.resource_database_id)
		WHEN 'DATABASE' THEN 'DB'
		ELSE
			CASE 
				WHEN TL1.resource_database_id = DB_ID() 
				THEN
					(
						select OBJECT_NAME(object_id, TL1.resource_database_id)
						from sys.partitions
						where hobt_id = TL1.resource_associated_entity_id
					)
				ELSE
					'(Run under DB context)'
			END
	END as ObjectName
	,TL1.resource_description
	,TL1.request_session_id
	,TL1.request_mode
	,TL1.request_status
	,WT.wait_duration_ms as [Wait Duration (ms)]
	,(
		select
			SUBSTRING(
				S.Text, 
				(ER.statement_start_offset / 2) + 1,
				((
					CASE 
						ER.statement_end_offset
					WHEN -1 
						THEN DATALENGTH(S.text)
						ELSE ER.statement_end_offset
					END - ER.statement_start_offset) / 2) + 1)		
		from 
			sys.dm_exec_requests ER 
				cross apply sys.dm_exec_sql_text(ER.sql_handle) S
		where
			TL1.request_session_id = ER.session_id
	 ) as [Query]
from
	sys.dm_tran_locks as TL1 left outer join sys.dm_os_waiting_tasks WT on
		TL1.lock_owner_address = WT.resource_address and TL1.request_status = 'WAIT'
where
	TL1.request_session_id <> @@SPID
order by
	TL1.request_session_id

Open in new window

0
 

Author Comment

by:enrique_aeo
ID: 33435900
very interesting, and could generate a block to see how it works your query
0
 
LVL 5

Expert Comment

by:robertg34
ID: 33438207

sp_who is a simple command to look for locking transactions.  Beyond that, here is a query that gives more information
from http://drsql.spaces.live.com/blog/cns!80677FB08B3162E4!906.entry
 
 

select  login_name,

        case des.transaction_isolation_level

            when 0 then 'Unspecified' when 1 then 'ReadUncomitted'

            when 2 then 'ReadCommitted' when 3 then 'Repeatable'

            when 4 then 'Serializable' when 5 then 'Snapshot'

        end as transaction_isolation_level,

        request_session_id, resource_type, resource_subtype, request_mode,         request_type, request_status, request_owner_type, 

        case when resource_type = 'object' then object_name(resource_associated_entity_id)

             when resource_type = 'database' then db_name(resource_associated_entity_id)

             when resource_type in ('key','page') then                                  (select object_name(object_id) from sys.partitions                                   where hobt_id = resource_associated_entity_id)

             else cast(resource_associated_entity_id as varchar(20))

        end

from sys.dm_tran_locks dtl

        left outer join sys.dm_exec_sessions des

            on dtl.request_session_id = des.session_id

where request_session_id <> @@spid

Open in new window

0
 

Author Comment

by:enrique_aeo
ID: 33442487
Hello, I have understood my question isas I can generate a lock on the database?
0
 
LVL 13

Expert Comment

by:dwkor
ID: 33442672
do you want to generate the lock? well. the siblest way is:
begin tran
  update table1 set.. where..

it will place exclusive locks on the updated records till you commit or rollback transaction
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

Author Comment

by:enrique_aeo
ID: 33442683
Hello, I made the query, but not blocking it generates no

begin tran
  update HumanResources.Employee
  set fechaRegistro = null
  where fechaRegistro is null
0
 
LVL 13

Expert Comment

by:dwkor
ID: 33442824
open the second session and run
select * from humanresouces.employee
0
 

Author Comment

by:enrique_aeo
ID: 33442843
first session
begin tran
  update HumanResources.Employee
  set fechaRegistro = null
  where fechaRegistro is null

second session
select * from HumanResources.Employee

 but not blocking it generates, i attached the results
resultSESSION.JPG
0
 
LVL 13

Assisted Solution

by:dwkor
dwkor earned 500 total points
ID: 33442867
are you sure your update statement updated any rows?
0
 

Author Closing Comment

by:enrique_aeo
ID: 33442938
That's right, the update did not update records, and i fixed it, thank you very much for your patience. Regards
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now