Pages

Wednesday, June 8, 2011

Locks in Database

Locks are mainly helpful to serialize jobs. Lock on a object is required by a session to update/edit the same, and if some other session wants to update it then it has to wait until earlier one releases the object and it comes to a consistent state.
This may be an object level enqueue to get the object edited by different sessions, or may be library lock/pin for the latch being used. But its dba who can decide what session should not be allowed to proceed or to be terminated taking others on priority.


Below are few of those tables and views that can help in identifying all kinds of locks
dba_ddl_locks
dba_dml_locks
gv$locked_object
gv$access
gv$lock
dba_kgllock


First four can show the details of the objects locked by some session. Such scenario of locks can show some error message like "ORA-04021: timeout occurred while waiting to lock object "

Following metalink note – 153994.1 below steps can be done in this case
1. Verify whether the package is locked by another user:
   SELECT * FROM v$access WHERE object = '&ObjectName';
   If there a row is returned, the package is still locked.
2. Use the SID which is returned and check in v$session which session is
   locking this package.
  
SELECT SID, SERIAL#, OSUSER, USERNAME FROM V$SESSION WHERE sid = '&SID';
3. If you are sure that it is ok to kill the session then use:
  
ALTER SYSTEM KILL SESSION '<SID>,<serial#>';

Similarly dba_ddl_locks and dba_dml_locks can also be checked to find out the locked objects and the session locking the same.
For the enqueues or blocks in database you can simply check below
SQL> select * from gv$lock where block = 1;
now you have the sid for the session and for 10g onwards you can use this sid as below
SQL> select inst_id, sid, serial# from gv$session where blocking_session = '<SID>';
So you have the details for the blocker and blocking session which can be further digged in with gv$session;

The description for the locks in the mode column is as below.
0 - 'None'
1 - 'Null'
2 - 'Row-S (SS)'
3 - 'Row-X (SX)'
4 - 'Share'
5 - 'Shared Row-X (SSX)'
6 - 'Exclusive'


All the description above can help you deal with locks and enqueues.

Click Here for some queries to check the enqueues and library locks for single and multinode databases.

No comments:

Post a Comment