ITPub博客

首页 > 数据库 > Oracle > [20140426]停止或暂停用户的sql语句的执行

[20140426]停止或暂停用户的sql语句的执行

原创 Oracle 作者:lfree 时间:2014-04-28 09:47:48 0 删除 编辑

[20140426]停止或者暂停用户的sql语句的执行.txt

有时候想停止或者暂停用户的sql语句的执行,当并不想退出,如果简单kill并不是很好的方式,昨天看了
http://yumianfeilong.com/html/2014/04/11/625.html
自己也做一些测试:

--会话1:
SCOTT@test01p> @ver
BANNER                                                                               CON_ID
-------------------------------------------------------------------------------- ----------
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production              0

SCOTT@test01p> select v.sid,v.serial#,p.spid from v$session v,v$process p  where v.paddr = p.addr and v.sid in (select sid from v$mystat where rownum=1);
       SID    SERIAL# SPID
---------- ---------- ------
        16        337 2924

--知道进程号,sid以及serial#.

1.测试1:

---会话1:
SCOTT@test01p> select * from dba_objects ;
..

---会话2:
SYS@test> exec dbms_system.set_ev(16,337,10237,1,'')
PL/SQL procedure successfully completed.

--可以发现会话1出现如下提示:
ERROR:
ORA-01013: user requested cancel of current operation
3000 rows selected.

--回到会话1执行(注意在本会话中第一次执行):
SCOTT@test01p> Select sysdate from dual ;
Select sysdate from dual
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation

---会话2:
SYS@test> exec dbms_system.set_ev(16,337,10237,0,'')
PL/SQL procedure successfully completed.

--回到会话1执行:
SCOTT@test01p> Select sysdate from dual ;
SYSDATE
-------------------
2014-04-26 20:58:27

--可以发现通过设置10237事件,可以中断用户sql语句的执行.

2.测试2:
--同样使用oradebug 也可以达到相同的效果:

---会话2:
SYS@test> oradebug setospid 2924
Oracle pid: 36, Windows thread id: 2924, image: ORACLE.EXE (SHAD)
SYS@test> oradebug session_event 10237 trace name context forever, level 1;
Statement processed.

--回到会话1:
SCOTT@test01p> select * from dba_objects ;
select * from dba_objects
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation

SCOTT@test01p> Select sysdate from dual ;
SYSDATE
-------------------
2014-04-26 21:03:52
--但是可以发现Select sysdate from dual ;可以执行成功.如果换成大写SELECT.
SCOTT@test01p> SELECT sysdate from dual ;
SELECT sysdate from dual
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation
--也就是如果会话1第1次执行成功的语句,不会出现ora_01013错误.

----回到会话2:
SYS@test> oradebug session_event 10237 trace name context forever, level 0;
Statement processed.
或者
SYS@test> oradebug session_event 10237 trace name context off;
Statement processed.

----回到会话1:
SCOTT@test01p> select * from dept ;
--等待结束.

----回到会话2:
SYS@test> oradebug session_event 10237 trace name context forever, level 1;
Statement processed.


----回到会话1:
SCOTT@test01p> select * from dept ;
select * from dept
*
ERROR at line 1:
ORA-01013: user requested cancel of current operation

SCOTT@test01p> Select sysdate from dual ;
SYSDATE
-------------------
2014-04-26 21:14:20
--可以发现select * from dept ;执行失败,但是执行Select sysdate from dual ;成功,也许dual表特殊,而且执行的语句实际上不访问该表.

3.换一个方法测试:
----回到会话2:
SYS@test> oradebug session_event 10237 trace name context forever, level 0;
Statement processed.
或者
SYS@test> oradebug session_event 10237 trace name context off;
Statement processed.

----回到会话1:
SCOTT@test01p> select dummy from dual ;
D
-
X

----回到会话2:
SYS@test> oradebug session_event 10237 trace name context forever, level 1;
Statement processed.

SCOTT@test01p> select dummy from dual ;
select dummy from dual
                  *
ERROR at line 1:
ORA-01013: user requested cancel of current operation
--可以发现确实这样,

4.暂停sql语句执行:
SQL_DBA > oradebug suspend;
Statement processed.

SQL_DBA > oradebug resume;
Statement processed.

也可以在linux下使用kill命令完成:
kill -l 可以显示signal.
# kill -l
1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE
9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2
13) SIGPIPE     14) SIGALRM     15) SIGTERM     17) SIGCHLD
18) SIGCONT     19) SIGSTOP     20) SIGTSTP     21) SIGTTIN
22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO
30) SIGPWR      31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1
36) SIGRTMIN+2  37) SIGRTMIN+3  38) SIGRTMIN+4  39) SIGRTMIN+5
40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8  43) SIGRTMIN+9
44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13
52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9
56) SIGRTMAX-8  57) SIGRTMAX-7  58) SIGRTMAX-6  59) SIGRTMAX-5
60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2  63) SIGRTMAX-1
64) SIGRTMAX

-9 => 对应的就是SIGKILL.

很明显 18) SIGCONT     19) SIGSTOP 相对应.

这样如果kill -19 ,可以使对应进程挂起,或者停止.而 kill -18 可以让对应进程继续.

同样在windows下使用systeminternals自带的procexp.exe的工具,找到tid一样可以实现相同的功能.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/267265/viewspace-1150645/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
熟悉oracle相关技术,擅长sql优化,rman备份与恢复,熟悉linux shell编程。

注册时间:2008-01-03

  • 博文量
    4006
  • 访问量
    7969769