Thursday, March 28, 2013

MS SQL Server - DBA - DMV - Find out DB/table lock


Environment:
SQL Server 2008 R2 - DMV (Dynamic Management View)


Query to see the locks across database:

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,
        L.request_status AS LockStatus,
        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

Note:
  1. This query will run using selected database - db_id().
  2. sys.dm_tran_locks - system table has two types of columns (resource and request). The resource group describes the resource on which the lock request is being made, and the request group describes the lock request. 
  3. Values in sys.dm_tran_locks.resource_type - KEY, PAGE, DATABASE, FILE, OBJECT etc
  4. Values in sys.dm_tran_locks.request_mode - 
    • NULL = No access is granted to the resource. Serves as a placeholder.
    • Sch-S = Schema stability. Ensures that a schema element, such as a table or index, is not dropped while any session holds a schema stability lock on the schema element.
    • Sch-M = Schema modification. Must be held by any session that wants to change the schema of the specified resource. Ensures that no other sessions are referencing the indicated object
    • S = Shared. The holding session is granted shared access to the resource.
    • U = Update. Indicates an update lock acquired on resources that may eventually be updated. It is used to prevent a common form of deadlock that occurs when multiple sessions lock resources for potential update at a later time.
    • X = Exclusive. The holding session is granted exclusive access to the resource.
    • IS = Intent Shared. Indicates the intention to place S locks on some subordinate resource in the lock hierarchy.
    • IU = Intent Update. Indicates the intention to place U locks on some subordinate resource in the lock hierarchy.
    • IX = Intent Exclusive. Indicates the intention to place X locks on some subordinate resource in the lock hierarchy.
    • SIU = Shared Intent Update. Indicates shared access to a resource with the intent of acquiring update locks on subordinate resources in the lock hierarchy.
    • SIX = Shared Intent Exclusive. Indicates shared access to a resource with the intent of acquiring exclusive locks on subordinate resources in the lock hierarchy.
    • UIX = Update Intent Exclusive. Indicates an update lock hold on a resource with the intent of acquiring exclusive locks on subordinate resources in the lock hierarchy.
    • BU = Bulk Update. Used by bulk operation
    • There are few others about range related
  5. Values in sys.dm_tran_locks.request_status - 
    • GRANT: the lock request was granted
    • WAIT: The request to acquire a particular lock type is waiting.
    • CONVERT: the request was granted earlier with a particular lock status but now is trying to upgrade to another status and is being blocked
Query to find lock on a table:

SELECT REQUEST_MODE, REQUEST_TYPE, REQUEST_SESSION_ID FROM
sys.dm_tran_locks
WHERE RESOURCE_TYPE = 'OBJECT'
AND RESOURCE_ASSOCIATED_ENTITY_ID =(SELECT OBJECT_iD('name_of_the_table'))
ORDER BY request_mode;

Note:
  1. Have to change 'name_of_the_table' to correct table
Kill a open session:
Command - KILL ;

Note: 
  1. sessionid can be found by running above queries.  
  2. Most of the time killing exclusive (X) type request solves table/db lock issue
Resources
1. http://social.msdn.microsoft.com/Forums/en-US/sqlgetstarted/thread/cae67b92-1f17-4f97-b68e-cfaba1207253/
2. http://msdn.microsoft.com/en-us/library/ms190345.aspx

Wednesday, March 20, 2013

Windows Administration - Error - External Hard Drive access denied

Error: 
I have Western Digital HDD My Passport. I always plug it in before work and properly remove/eject (using windows 'Safely remove hardware and eject media' feature). All on a sudden I have started getting error 'Access Denied' (see screenshot-01).


Screenshot-01: Access Denied













Solution:

Option-04 below is the solution. But I had to do that in 'Safe Mode' to make it work. 

Options tried to get rid of this error:

Option-01: Removed and plugged-in again
Result: Didn't work

Option-02: Restart the machine
Result: Didn't work

Option-03: Change drive letter
Steps:

  1. Go to Computer Management (Start - Control Panel - Administrative tools - Computer Management)
  2. Go to Disk Management option (From left navigation panel, click Disk Management under Storage)
  3. Select the disk which is having access denied error
  4. Right Mouse button - Change drive letter and paths - Change...
  5. Select different drive letter from option under 'Assign the following drive letter' (in my case I have selected H)
Screenshot-02: Computer Management
Result: Didn't work



Option-04: Take ownership and change permission using windows command

Steps:

  1. I have another post to set ownership and permission 'Setup ownership and permission to file/folder/drive'. 
Result: getting 'Access Denied' error in the command prompt for both the commands


Resources:

Windows Administration - Setup - Set ownership and permission to file/folder/drive letter




Environment: Windows 7



Trying to set ownership and permission to my external drive

Take ownership - using command prompt 

Steps:
  1. Click Start , click All programs , and click Accessories
  2. Right-click Command Prompt and click Run as administrator
  3. In Administrator: Command Prompt window
  4. Type command "takeown /F G:"

Take ownership - using ui 
Steps:
  1. Select drive (G) - right mouse button - select Properties
  2. Go to 'Security' tab - Click 'Advanced' button (see screenshot-01)
  3. It will open 'Advanced Security Settings' wizard 
  4. Select Owner tab 
  5. Click 'Edit...' button (see screenshot-02)
  6. Next screen might display 'Administrator' and current user in the 'Change owner to' list
  7. If it's needed to set owner to different user, click 'Other users and groups' button (see screenshot-03)
  8. Enter correct username or group name and hit enter in 'Select User, Computer, Service Account or Group' screen

Set permission - using command prompt 

Steps:
  1. Click Start , click All programs , and click Accessories
  2. Right-click Command Prompt and click Run as administrator
  3. In Administrator: Command Prompt window
  4. Type command "CACLS G: /G Administrator:F" [meaning of command: give full permission (F) to user (Administrator) for drive G)

Set permission - using ui 
Steps:
  1. Select drive (G) - right mouse button - select Properties
  2. Go to 'Security' tab - Click 'Advanced' button (see screenshot-01)
  3. It will open 'Advanced Security Settings' wizard 
  4. Go to 'Permission' tab
  5. Click 'Configure' button (see screenshot-04)
  6. In next screen, to add new user, click 'Add' button (see screenshot-05)
  7. To to modify permission attributes, select the user from the list and click 'Edit...' button (see screenshot-06)

Screenshots:

Screenshot-01:






















Screenshot-02:



















Screenshot-03:















Screenshot-04: 



















Screenshot-05:




Screenshot-06:




























Resources: