增加异常处理

CREATE OR REPLACE PROCEDURE SOLVE_LOCK

AS

V_SQL VARCHAR2(3000); --定义 v_sql 接受抓取锁的sql语句
V_SQL02 VARCHAR2(3000);

KILL_SID NUMBER;
KILL_SERIAL NUMBER;

CUR_LOCK SYS_REFCURSOR; --定义游标变量,循环执行抓取锁的sql语句
CUR_LOCK02 SYS_REFCURSOR;

TYPE TP_LOCK IS RECORD( --定义 record类型的 变量
V_SID NUMBER,
V_TYPE VARCHAR2(10),
V_ID1 NUMBER,
V_ID2 NUMBER,
V_LMODE VARCHAR2(200),
V_REQUEST VARCHAR2(200),
V_LOCK_TIME NUMBER,
V_BLOCK NUMBER );
RECORDS_LOCK TP_LOCK;

TYPE TP_LOCK02 IS RECORD(
WAITING_SID NUMBER,
WAITING_SQL VARCHAR2(1000),
BLOCKER_EVENT VARCHAR2(1000),
BLOCKING_SID NUMBER,
BLOCKING_SQL VARCHAR2(1000));
RECORDS_LOCK02 TP_LOCK02;

V_BLOCKING_SID NUMBER;
V_WAITING_SID NUMBER;

BEGIN
 
DBMS_OUTPUT.PUT_LINE('------------------查找数据库中是否有锁阻塞、锁等待的情况------------------');
V_SQL:='SELECT SID,TYPE,ID1,ID2,
DECODE(LMODE,0, ''NONE'',1,''NULL'', 2, ''ROW SHARE'' ,3, ''ROW EXCLUSIVE'' ,4, ''SHARE'' ,5, ''SHARE ROW EXCLUSIVE'' ,6 ,''EXCLUSIVE'' ,''HAHA'') LOCK_TYPE,
DECODE(REQUEST,0, ''NONE'',1,''NULL'', 2, ''ROW SHARE'' ,3, ''ROW EXCLUSIVE'' ,4, ''SHARE'' ,5, ''SHARE ROW EXCLUSIVE'' ,6 ,''EXCLUSIVE'' ,''HAHA'') LOCK_REQUEST,
CTIME,BLOCK FROM V$LOCK WHERE TYPE IN (''TM'',''TX'')' ;

OPEN CUR_LOCK FOR V_SQL;
LOOP
FETCH CUR_LOCK INTO RECORDS_LOCK;
EXIT WHEN CUR_LOCK%NOTFOUND;

IF RECORDS_LOCK.V_REQUEST <> 'NONE' THEN --抓取发出请求锁的会话
DBMS_OUTPUT.PUT_LINE('waiting sid: '||RECORDS_LOCK.V_SID||' is request a lock ,lock_mode is '||RECORDS_LOCK.V_REQUEST||' and being locked '|| RECORDS_LOCK.V_LOCK_TIME||'s');
V_WAITING_SID:=RECORDS_LOCK.V_SID;
END IF;

IF RECORDS_LOCK.V_BLOCK <> 0 THEN --抓取发生锁阻塞的会话
DBMS_OUTPUT.put_line('blocking sid: '||RECORDS_LOCK.V_SID||' is make a lock , lock_mode is '||RECORDS_LOCK.V_LMODE);
V_BLOCKING_SID:=RECORDS_LOCK.V_SID;
END IF;

END LOOP;
CLOSE CUR_LOCK;

DBMS_OUTPUT.PUT_LINE('------------------查找产生锁的会话、锁等待的会话------------------------');
V_SQL02:='SELECT DISTINCT WAITER.SID "WAITING_SID",W_SQL.SQL_TEXT "SQL FROM WAITING SESSION",BLOCKER.EVENT "BLOCKER EVENT",BLOCKER.SID "BLOCKING SID",
B_SQL.SQL_TEXT "SQL FROM BLOCKING SESSION"  FROM V$SESSION WAITER, V$SESSION BLOCKER,V$SQL W_SQL,V$SQL B_SQL
WHERE WAITER.EVENT=''enq: TX - row lock contention'' AND WAITER.BLOCKING_SESSION=BLOCKER.SID
AND W_SQL.SQL_ID=WAITER.SQL_ID
AND B_SQL.SQL_ID =NVL(BLOCKER.SQL_ID,BLOCKER.PREV_SQL_ID)';

OPEN CUR_LOCK02 FOR V_SQL02;
LOOP  
FETCH CUR_LOCK02 INTO RECORDS_LOCK02;
EXIT WHEN CUR_LOCK02%NOTFOUND;

IF RECORDS_LOCK02.WAITING_SID IS NOT NULL THEN
DBMS_OUTPUT.put_line('waiting sid: '||RECORDS_LOCK02.WAITING_SID||' wait_sql is : '||RECORDS_LOCK02.WAITING_SQL);
END IF;

IF RECORDS_LOCK02.BLOCKING_SID IS NOT NULL THEN
DBMS_OUTPUT.put_line('blocking sid: '||RECORDS_LOCK02.BLOCKING_SID||' block_sql is : '||RECORDS_LOCK02.BLOCKING_SQL);
END IF;
END LOOP;
CLOSE CUR_LOCK02;

DBMS_OUTPUT.PUT_LINE('------------------解决 锁阻塞、锁等待------------------');

SELECT   SID,SERIAL# INTO KILL_SID,KILL_SERIAL FROM V$SESSION WHERE SID=V_BLOCKING_SID ;
   
DBMS_OUTPUT.put_line('ACTION: ALTER SYSTEM KILL SESSION ( '||KILL_SID||','||KILL_SERIAL||')');

EXCEPTION WHEN NO_DATA_FOUND THEN
  DBMS_OUTPUT.PUT_LINE(SQLCODE||SQLERRM);

END SOLVE_LOCK;

抓取锁的sql语句-第六次修改的更多相关文章

  1. 抓取锁的sql语句-第七次修改

    最近闲来没事,把之前写的那个抓取锁的存储过程重新修改.优化了一下,呵呵 create or replace procedure solve_lock_061203_wanjie(v_msg out v ...

  2. 抓取锁的sql语句-第五次修改

    CREATE OR REPLACE PROCEDURE SOLVE_LOCK AS V_SQL VARCHAR2(3000); --定义 v_sql 接受抓取锁的sql语句V_SQL02 VARCHA ...

  3. 抓取锁的sql语句-第四次修改

    --完成情况   变量V_BLOCKING_SID 用来动态抓取 产生锁的会话id,输出参数没有任何问题,但是执行报错  标识符无效! CREATE OR REPLACE PROCEDURE SOLV ...

  4. 抓取锁的sql语句-第三次修改

    CREATE OR REPLACE PROCEDURE SOLVE_LOCK AS V_SQL VARCHAR2(3000); --定义 v_sql 接受抓取锁的sql语句CUR_LOCK SYS_R ...

  5. 抓取锁的sql语句-第二次修改

    CREATE OR REPLACE PROCEDURE SOLVE_LOCK AS V_SQL VARCHAR2(3000);  --定义 v_sql 接受抓取锁的sql语句 CUR_LOCK SYS ...

  6. 抓取锁的sql语句-第一次修改

    CREATE OR REPLACE PROCEDURE SOLVE_LOCK AS V_SQL VARCHAR2(3000); CUR_LOCK SYS_REFCURSOR; TYPE TP_LOCK ...

  7. tcpdump来抓取执行的sql语句

    # tcpdump -n -nn -tttt -i eth1 -s 65535 'port 3306' -w tcpdump_mysql.ret -C 100 一个TCP包中包含多个mysql协议包, ...

  8. Oracle数据库查找持有锁的SQL语句,而不是请求锁的SQL语句(原创)

    Oracle数据库查找持有锁的SQL语句,而不是请求锁的SQL语句 查找活动的事务以及活动事务关联的会话信息 select s.sid 会话ID, s.serial# 会话序列号, s.usernam ...

  9. 数据库-SQL语句:删除和修改语句-列类型-列约束

    使用MySQL客户端连接服务器的两种方式: (1)交互模式: ——查 mysql.exe  -h127.0.0.1  -uroot  -p mysql   -uroot (2)脚本模式:——增删改 m ...

随机推荐

  1. Popular Deep Learning Tools – a review

    Popular Deep Learning Tools – a review Deep Learning is the hottest trend now in AI and Machine Lear ...

  2. codeforces C. Sereja and Swaps

    http://codeforces.com/contest/426/problem/C 题意:找出连续序列的和的最大值,可以允许交换k次任意位置的两个数. 思路:枚举区间,依次把区间内的比较小的数换成 ...

  3. insert 加的锁

    ?INSERT sets an exclusive lock on the inserted row. This lock is an index-record lock, not a next-ke ...

  4. 【HDOJ】2699 Five in a Row

    wa了几次,至少要考虑4个方向:下.右.左下.右下.很像当年北航的机试题目. /* 2699 */ #include <iostream> #include <cstdio> ...

  5. Linux目录树

    Linux目录树(directory tree) 分层结构(不同于数据库文件系统),单个文件/目录的最大长度为255个字符,完整路径为4096个字符 特殊的文件系统 文件系统 挂载点 说明 Root ...

  6. MongoDB主从配置

    master的配置 # cat mongod.conf dbpath = /app/sinova/mongodata/db            #指定数据库目录 logpath = /app/sin ...

  7. 【转】文件读写NDK(或Linux)

    原文网址:http://www.ithao123.cn/content-10709539.html 使用NDK进行文件读写,有利于保存数据的安全性,项目需要,要文件读写从Java中处理搬到Linux平 ...

  8. 加密混淆.Net程序

    记录在这里,以便学习使用! 一 加密混淆.Net程序 打开Intellilock进处此界面 点击add按钮加入你要操作的程序集, Merge Assemblie 可以合并当前程序集列表中的程序集为一个 ...

  9. 一个js变量以及其作用域的源码示例

    今天遇到了一个问题,抽象出来的代码如下: var zoom=13; function setZoom(){ zoom=14; } function displayZoom(){ this.setZoo ...

  10. Swing实现文件选择(目录选择)附导出

    具体生成工具如图: (1) (2) (3) (4) 源码 : example.java package org.qiailin.jframe; import java.awt.Container; i ...