[20140426]停止或暫停使用者的sql語句的執行

lfree發表於2014-04-28

[20140426]停止或者暫停使用者的sql語句的執行.txt

有時候想停止或者暫停使用者的sql語句的執行,當並不想退出,如果簡單kill並不是很好的方式,昨天看了

自己也做一些測試:

--會話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部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-1150645/,如需轉載,請註明出處,否則將追究法律責任。

相關文章