SQL> create table dept_1
2 as select * from dept;
Table created.
SQL> create table empt_1
2 as select * from emp;
Table created.
SQL> alter table dept_1
2 add constraint dept_1_pk
3 primary key(deptno);
Table altered.
SQL> alter table emp_1
2 add constraint emp_1_pk
3 primary key(deptno);
Table altered.
SQL> alter table emp_1
2 add constraint emp_1_fk_dept
3 foreign key(deptno)
4 references dept_1(deptno);
Table altered.
SQL> create index emp_1_deptno_idx
2 on emp_1(deptno);
Index created.
SQL> update dept_1
2 set dname = initcap(dname);
4 rows updated.
SQL> select username,
v$lock.sid,
trunc(id1/power(2,16)) rbs,
bitand(id1,to_number('ffff','xxxx'))+0 slot,
id2 seq,
lmode,
request
from v$lock, v$session
where v$lock.type = 'TX'
and v$lock.sid = v$session.sid
and v$session.username = USER;
USERNAME SID RBS SLOT SEQ LMODE REQUEST
--------------- ---------- ---------- ---------- ---------- ---------- ----------
UBACK 1088 3 14 457984 6 0
SQL> select XIDUSN, XIDSLOT, XIDSQN
2 from v$transaction;
XIDUSN XIDSLOT XIDSQN
---------- ---------- ----------
3 14 457984
SQL> update emp_1 set ename = upper(ename);
14 rows updated.
SQL> update dept_1 set deptno = deptno-10;
4 rows updated.
SQL> select username,
v$lock.sid,
trunc(id1/power(2,16)) rbs,
bitand(id1,to_number('ffff','xxxx'))+0 slot,
id2 seq,
lmode,
request
from v$lock, v$session
where v$lock.type = 'TX'
and v$lock.sid = v$session.sid
and v$session.username = USER;
USERNAME SID RBS SLOT SEQ LMODE REQUEST
----------- ---------- ---------- ---------- ---------- ---------- ----------
UBACK 1043 3 11 457999 0 6
UBACK 1043 8 38 451952 6 0
UBACK 1088 3 11 457999 6 0
SQL> select XIDUSN, XIDSLOT, XIDSQN
2 from v$transaction;
XIDUSN XIDSLOT XIDSQN
---------- ---------- ----------
8 38 451952
3 11 457999
SQL> select
(select username from v$session where sid=a.sid) blocker,
a.sid,
' is blocking ',
(select username from v$session where sid=b.sid) blockee,
b.sid
from v$lock a, v$lock b
where a.block = 1
and b.request > 0
and a.id1 = b.id1
and a.id2 = b.id2;
BLOCKER SID 'ISBLOCKING' BLOCKEE SID
------------------------------ ---------- ------------- ------------------------------ ----------
UBACK 1088 is blocking UBACK 1043
SQL> select username,
v$lock.sid,
trunc(id1/power(2,16)) rbs,
bitand(id1,to_number('ffff','xxxx'))+0 slot,
id2 seq,
lmode,
request
from v$lock, v$session
where v$lock.type = 'TX'
and v$lock.sid = v$session.sid
and v$session.username = USER;
USERNAME SID RBS SLOT SEQ LMODE REQUEST
------------- ---------- ---------- ---------- ---------- ---------- ----------
UBACK 1043 8 38 451952 6 0
SQL> select XIDUSN, XIDSLOT, XIDSQN
2 from v$transaction;
XIDUSN XIDSLOT XIDSQN
---------- ---------- ----------
8 38 451952
SQL> create table t
2 ( x int primary key,
3 y varchar2(4000)
4 )
5 /
Table created.
SQL> insert into t (x, y)
2 select rownum, rpad('*', 148, '*')
3 from dual
4 connect by level <= 46
46 rows created.
SQL> select length(y),
2 dbms_rowid.rowid_block_number(rowid) blk,
3 count(*), min(x), max(x)
4 from t
5 group by length(y), dbms_rowid.rowid_block_number(rowid);
LENGTH(Y) BLK COUNT(*) MIN(X) MAX(X)
--------- ------ -------- -------- -------
148 4599 46 1 46
SQL> create or replace procedure do_update(p_n in number)
2 as
3 pragma autonomous_transaction;
4 l_rec t%rowtype;
5 resource_busy exception;
6 pragma exception_init(resource_busy, -54);
7 begin
8 select *
9 into l_rec
10 from t
11 where x = p_n
12 for update NOWAI
14 do_update( p_n+1);
15 commit;
16 exception
17 where resource_busy
18 then
19 dbms_output.put_line('locked out trying to select row '|| p_n);
20 commit;
21 when no_data_found
22 then
23 dbms_output.put_line('we finished - no problems ;);
24 commit;
25 end;
26 /
procedure created.
SQL> exec do_update(1);
locked out trying to select row 38
PL/SQL procedure successfully completed
SQL> truncate table t;
Table truncated.
SQL> insert into t (x,y)
2 select rownum, rpad('*', 147, '*')
3 from dual
4 connect by level <= 46;
46 rows created
SQL> select length(y)
2 dbms_rowid.rowid_block_number(rowid) blk,
3 count(*), min(x), max(x)
4 from t
5 group by length(y), dbms_rowid.rowid_block_number(rowid);
LENGTH(Y) BLK COUNT(*) MIN(X) MAX(X)
--------- ------ -------- -------- -------
147 4663 46 1 46
SQL> exec do_update(1);
we finish - no problem
PL/SQL procedure successfully completed.
|Session1|
|SQL> select * from dept_1 for update nowait;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> update dept_1 set deptno = deptno-10;
4 rows updated.|
Session2|
SQL> drop table dept_1;
drop table dept_1
ERROR at line 1:
ORA-00054: resource busy and acquire with NOWAIT specified|
SQL> create table t1 ( x int );
Table created.
SQL> create table t2 ( x int );
Table created.
SQL> insert into t1 values ( 1 );
1 row created.
SQL> insert into t2 values ( 1 );
1 row created.
SQL> select (select username
from v$session
where sid = v$lock.sid) username,
sid,
id1,
id2,
lmode,
request, block, v$lock.type
from v$lock
where sid = (select sid
from v$mystat
where rownum=1)
/
USERNAME SID ID1 ID2 LMODE REQUEST BLOCK TY
--------------- ---------- ---------- ---------- ---------- ---------- ---------- --
UBACK 1088 217429 0 3 0 0 TM
UBACK 1088 217430 0 3 0 0 TM
UBACK 1088 655400 457568 6 0 0 TX
SQL> select object_name, object_id
2 from user_objects
3 where object_name in ('T1','T2');
OBJECT_NAME OBJECT_ID
-------------- ----------
T1 217429
T2 217430
- 강좌 URL : http://www.gurubee.net/lecture/4409
- 구루비 강좌는 개인의 학습용으로만 사용 할 수 있으며, 다른 웹 페이지에 게재할 경우에는 출처를 꼭 밝혀 주시면 고맙겠습니다.~^^
- 구루비 강좌는 서비스 제공을 위한 목적이나, 학원 홍보, 수익을 얻기 위한 용도로 사용 할 수 없습니다.