Discussion Locked

Determinine the blocking session user

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Using Oracle 8i, a table row is locked by a 'FOR UPDATE' clause based on a unique rowid. How can the user id of the session that has the row locked be determined? There can be other sessions waiting to update the same row.

7 replies

by sparent

Lock information is kept in the V$LOCK views, specifically:

V$LOCK
V$LOCKED_OBJECT
V$LOCKS_WITH_COLLISIONS
V$LOCK_ACTIVITY
V$LOCK_ELEMENT

Now, the problem isthat information is pretty cryptic and hard to use. Either use a tool, like SQL-Lab, that makes sense of it, or borrow an Oracle DBA book for examples.

Please note that access to these views are typically not available publicly.

Another optionis to try the SET TRANSACTION ISOLATION LEVEL SERIALIZABLE command. I've been unable to use it succesfully but you may be more fortunate. This command changes the isolation level from READ COMMITTED to SERIALIZABLE. This will mean that the SQL command that's trying to access a locked resource will fail, instead of waiting for the lock to be released.

Good luck!

by seefet

Thanks srparent!
I have investigated the LOCK views. Now all I need to do is figure out how to use them to find that elusive "blocking session user".

by ambily

The sql below should help you to find out the users who are locking resources:

SELECT x.serial#, x.sid,x.username,y.id1
FROM v$session x, v$lock y
WHERE y.id1 IN
(SELECT distinct b.id1 FROM v$session a , v$lock b
WHERE b.kaddr = a.lockwait)
AND x.sid = y.sid
AND y.request = 0;

Hope that helps

by ambily

This sql will give you the sql statement also of the blocking users if they have not committed:

SELECT x.serial#, x.sid,x.username,y.id1,z.sql_text SQL
FROM v$session x, v$lock y, v$sqltext z
WHERE y.id1 IN
(SELECT distinct b.id1
FROM v$session a , v$lock b , v$sqltext z
WHERE b.kaddr = a.lockwait)
AND x.sid = y.sid
AND z.hash_value = x.sql_hash_value
AND y.request = 0;

Good luck!!

by seefet

Thankam Nair,

Thank you!! Your code example can be incorporated into our Procedure with very little modification to determine the locking user for a given table row.

seefet

This discussion is closed to new replies.