1. 程式人生 > >v$session v$lock v$locked_object

v$session v$lock v$locked_object

讀蓋老師的書,看到了一個例子.自己動手做了一下

開啟一個session執行以下語句
[email protected]>select * from emp for update;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

14 rows selected.
開另一個session執行update語句,出現等待。

[email protected]>update emp set empno=1 where empno=7788;

 我們需要檢視當我們執行 update emp set empno=1 where empno=7788 語句的時候在等待什麼

select sid,serial#,event,sql_text from v$session s,v$sql q where s.sql_id=q.sql_id and q.SQL_TEXT like 'update emp set empno=1 where empno=7788%';
	
SID SERIAL#	EVENT                         SQL_TEXT


27	35	    enq: TX - row lock contention	update emp set empno=1 where empno=7788

檢視是哪個會話導致的

SELECT SID,BLOCKING_INSTANCE,BLOCKING_SESSION FROM v$session WHERE SID=27;


SID BLOCKING_INSTANCE BLOCKING_SESSION
27	1	                39

kill這個會話

select sid,serial# from v$session s where s.sid=39;

sid   serial#
39    39


alter system kill session '39,39';

然後看到語句更新成功

[email protected]
>update emp set empno=1 where empno=7788; 1 row updated.

2.其他方法

	[email protected]>select sid,type,id1,id2 from v$lock where sid=27;

       SID TY        ID1        ID2
---------- -- ---------- ----------
        27 TX      65537       1205
        27 AE        100          0
        27 TM      77289          0
	[email protected]>select sid,type from v$lock where id1=65537 and id2=1205;

       SID TY
---------- --
        27 TX
        39 TX

然後殺掉這個會話