Monday, October 6, 2008

Display User Lock Information

In general, multiuser databases use some form of data locking to solve the problems associated with data concurrency, consistency, and integrity. Locks are mechanisms that prevent destructive interaction between transactions accessing the same resource.

Create a blocking lock
create table tstlock (foo varchar2(1), bar varchar2(1));
insert into tstlock values (1,'a');
insert into tstlock values (2, 'b');

Now grab a lock on the whole table, still in Session 1:
SQL> select * from tstlock for update ;

And in Session 2, try to update a row:
SQL> update tstlock set bar='a' where bar='a' ;


To display locks being held (or waited on) in the database,
set echo off
set pagesize 60
Column SID FORMAT 999 heading "Sess|ID "
COLUMN OBJECT_NAME FORMAT A17 heading "OBJ NAME or|TRANS_ID" Trunc
COLUMN OSUSER FORMAT A10 heading "Op Sys|User ID"
COLUMN USERNAME FORMAT A8
COLUMN TERMINAL FORMAT A8 trunc

select B.SID, C.USERNAME, C.OSUSER, C.TERMINAL, DECODE(B.ID2, 0, A.OBJECT_NAME,
'Trans-'||to_char(B.ID1)) OBJECT_NAME, B.TYPE,
DECODE(B.LMODE,0,'--Waiting--', 1,'Null', 2,'Row Share', 3,'Row Excl', 4,'Share', 5,'Sha Row Exc', 6,'Exclusive', 'Other') "Lock Mode",
DECODE(B.REQUEST,0,' ', 1,'Null', 2,'Row Share', 3,'Row Excl', 4,'Share', 5,'Sha Row Exc', 6,'Exclusive', 'Other') "Req Mode"
from DBA_OBJECTS A, V$LOCK B, V$SESSION C
where A.OBJECT_ID(+) = B.ID1 and B.SID = C.SID and C.USERNAME is not null
order by B.SID, B.ID2;

No comments:

search engine

Custom Search