CURSOR c1 IS
SELECT x,y,z
FROM a
WHERE x = nnn
FOR UPDATE;
OPEN c1 etc ...
Can I handle the fact that the target record might be locked, in such a way to warn the user (rather than have them sit and wait for the other session to commit or rollback)?
Sounds fine. The EXCEPTION_INIT pragma is a useful way of associating names with given error numbers such that you can use the name (rather than the number) in the exception section.
Code:
DECLARE
resource_busy EXCEPTION;
PRAGMA EXCEPTION_INIT (resource_busy, -54);
BEGIN
SELECT ...
FROM ...
FOR UPDATE NOWAIT;
EXCEPTION
WHEN resource_busy THEN
do_something_else;
END;
In 9i you can use FOR UPDATE WAIT n; where n is the seconds to wait for a lock before raising error. If locks are held briefly this may avoid repeated attempts by the user to obtain the record.
CURSOR c1 IS
SELECT ...
FROM ...
FOR UPDATE WAIT 30;
BEGIN
OPEN c1;
FETCH c1 etc ...
UPDATE ...
SET ...
WHERE CURRENT OF c1;
EXCEPTION
WHEN resource_busy THEN
RAISE_APPLICATION_ERROR (-20001, ...
WHEN others THEN
RAISE_APPLICATION_ERROR (-20002, ...
END;
I lock the target record in another session, then run my PL\SQL. It waits 30 seconds then returns with Error -20002 and not - 20001 as expected.
Bookmarks