自動殺掉堵塞別人超過2小時且自身是不活動的會話
--先建立sqltext_kill_records和record_auto_kill_sessions表,後者使用create table record_auto_kill_sessions as select 'for迴圈中的語句,最後一個欄位加上sysdate'
--grant select on v_$session to dba_monitor;
--grant alter system to dba_monitor;
--再用job呼叫這個procedure就可以了
CREATE or REPLACE PROCEDURE proc_auto_kill_session AS
sqltext VARCHAR2(200) := '';
--ddl_cursor integer;
BEGIN
--check if existing any session which is blocking others
for cur_ses_locks in (select username,
sid,
serial#,
status,
LOGON_TIME,
seconds_in_wait,
LAST_CALL_ET,
sql_id,
PREV_SQL_ID,
event,
PROGRAM,
CLIENT_IDENTIFIER,
machine,
action,
PROCESS,
osuser
from v$session
where sid in (select FINAL_BLOCKING_SESSION
from v$session
where state = 'WAITING'
and BLOCKING_SESSION_STATUS = 'VALID'
and FINAL_BLOCKING_SESSION_STATUS = 'VALID')
and status = 'INACTIVE'
and sql_id is null
)
loop
--if the blocker is inactive form session which has blocked others > 600s then kill immediately
if cur_ses_locks.status = 'INACTIVE' and cur_ses_locks.event like 'SQL*Net message from client%' and cur_ses_locks.seconds_in_wait > 7200 then
sqltext := 'ALTER SYSTEM KILL SESSION ' || '''' || cur_ses_locks.sid || ',' || cur_ses_locks.serial# || '''';
--ddl_cursor := dbms_sql.open_cursor;
--dbms_sql.parse(ddl_cursor, sqltext, dbms_sql.native);
EXECUTE IMMEDIATE sqltext;
insert into dba_monitor.record_auto_kill_sessions
values
(cur_ses_locks.username
cur_ses_locks.sid,
cur_ses_locks.serial#,
cur_ses_locks.status,
cur_ses_locks.LOGON_TIME,
cur_ses_locks.seconds_in_wait,
cur_ses_locks.LAST_CALL_ET,
cur_ses_locks.sql_id,
cur_ses_locks.PREV_SQL_ID,
cur_ses_locks.event,
cur_ses_locks.PROGRAM,
cur_ses_locks.CLIENT_IDENTIFIER,
cur_ses_locks.machine,
cur_ses_locks.action
cur_ses_locks.PROCESS
cur_ses_locks.osuser
sysdate);
insert into dba_monitor.sqltext_kill_records (script) values (sqltext);
commit;
end if;
end loop;
END;
下面這個procedure也不錯
CREATE or REPLACE PROCEDURE proc_auto_kill_session AS
sqltext VARCHAR2(200) := '';
--ddl_cursor integer;
BEGIN
--check if existing any session which is blocking others
for cur_ses_locks in (select r.root_sid,
s.serial#,
s.client_identifier,
r.blocked_num,
r.avg_wait_seconds,
s.username,
s.status,
s.event,
s.machine,
s.program,
s.sql_id,
s.prev_sql_id
from (select root_sid,
avg(seconds_in_wait) as avg_wait_seconds,
count(*) - 1 as blocked_num
from (select CONNECT_BY_ROOT sid as root_sid,
seconds_in_wait
from v$session
start with blocking_session is null
connect by prior sid = blocking_session)
group by root_sid
having count(*) > 1) r,
v$session s
where r.root_sid = s.sid
)
loop
--if the blocker is inactive form session which has blocked others > 600s then kill immediately
if cur_ses_locks.status = 'INACTIVE' and cur_ses_locks.event like 'SQL*Net message from client%' and cur_ses_locks.avg_wait_seconds > 1800 then
sqltext := 'ALTER SYSTEM KILL SESSION ' || '''' || cur_ses_locks.root_sid || ',' || cur_ses_locks.serial# || '''';
--ddl_cursor := dbms_sql.open_cursor;
--dbms_sql.parse(ddl_cursor, sqltext, dbms_sql.native);
EXECUTE IMMEDIATE sqltext;
insert into dba_monitor.record_auto_kill_sessions
values
(cur_ses_locks.root_sid,
cur_ses_locks.serial#,
cur_ses_locks.client_identifier,
cur_ses_locks.blocked_num,
cur_ses_locks.avg_wait_seconds,
cur_ses_locks.username,
cur_ses_locks.status,
cur_ses_locks.event,
cur_ses_locks.machine,
cur_ses_locks.program,
cur_ses_locks.sql_id,
cur_ses_locks.prev_sql_id,
sysdate);
insert into dba_monitor.sqltext_kill_records (script) values (sqltext);
commit;
end if;
end loop;
END;
--grant select on v_$session to dba_monitor;
--grant alter system to dba_monitor;
--再用job呼叫這個procedure就可以了
CREATE or REPLACE PROCEDURE proc_auto_kill_session AS
sqltext VARCHAR2(200) := '';
--ddl_cursor integer;
BEGIN
--check if existing any session which is blocking others
for cur_ses_locks in (select username,
sid,
serial#,
status,
LOGON_TIME,
seconds_in_wait,
LAST_CALL_ET,
sql_id,
PREV_SQL_ID,
event,
PROGRAM,
CLIENT_IDENTIFIER,
machine,
action,
PROCESS,
osuser
from v$session
where sid in (select FINAL_BLOCKING_SESSION
from v$session
where state = 'WAITING'
and BLOCKING_SESSION_STATUS = 'VALID'
and FINAL_BLOCKING_SESSION_STATUS = 'VALID')
and status = 'INACTIVE'
and sql_id is null
)
loop
--if the blocker is inactive form session which has blocked others > 600s then kill immediately
if cur_ses_locks.status = 'INACTIVE' and cur_ses_locks.event like 'SQL*Net message from client%' and cur_ses_locks.seconds_in_wait > 7200 then
sqltext := 'ALTER SYSTEM KILL SESSION ' || '''' || cur_ses_locks.sid || ',' || cur_ses_locks.serial# || '''';
--ddl_cursor := dbms_sql.open_cursor;
--dbms_sql.parse(ddl_cursor, sqltext, dbms_sql.native);
EXECUTE IMMEDIATE sqltext;
insert into dba_monitor.record_auto_kill_sessions
values
(cur_ses_locks.username
cur_ses_locks.sid,
cur_ses_locks.serial#,
cur_ses_locks.status,
cur_ses_locks.LOGON_TIME,
cur_ses_locks.seconds_in_wait,
cur_ses_locks.LAST_CALL_ET,
cur_ses_locks.sql_id,
cur_ses_locks.PREV_SQL_ID,
cur_ses_locks.event,
cur_ses_locks.PROGRAM,
cur_ses_locks.CLIENT_IDENTIFIER,
cur_ses_locks.machine,
cur_ses_locks.action
cur_ses_locks.PROCESS
cur_ses_locks.osuser
sysdate);
insert into dba_monitor.sqltext_kill_records (script) values (sqltext);
commit;
end if;
end loop;
END;
下面這個procedure也不錯
CREATE or REPLACE PROCEDURE proc_auto_kill_session AS
sqltext VARCHAR2(200) := '';
--ddl_cursor integer;
BEGIN
--check if existing any session which is blocking others
for cur_ses_locks in (select r.root_sid,
s.serial#,
s.client_identifier,
r.blocked_num,
r.avg_wait_seconds,
s.username,
s.status,
s.event,
s.machine,
s.program,
s.sql_id,
s.prev_sql_id
from (select root_sid,
avg(seconds_in_wait) as avg_wait_seconds,
count(*) - 1 as blocked_num
from (select CONNECT_BY_ROOT sid as root_sid,
seconds_in_wait
from v$session
start with blocking_session is null
connect by prior sid = blocking_session)
group by root_sid
having count(*) > 1) r,
v$session s
where r.root_sid = s.sid
)
loop
--if the blocker is inactive form session which has blocked others > 600s then kill immediately
if cur_ses_locks.status = 'INACTIVE' and cur_ses_locks.event like 'SQL*Net message from client%' and cur_ses_locks.avg_wait_seconds > 1800 then
sqltext := 'ALTER SYSTEM KILL SESSION ' || '''' || cur_ses_locks.root_sid || ',' || cur_ses_locks.serial# || '''';
--ddl_cursor := dbms_sql.open_cursor;
--dbms_sql.parse(ddl_cursor, sqltext, dbms_sql.native);
EXECUTE IMMEDIATE sqltext;
insert into dba_monitor.record_auto_kill_sessions
values
(cur_ses_locks.root_sid,
cur_ses_locks.serial#,
cur_ses_locks.client_identifier,
cur_ses_locks.blocked_num,
cur_ses_locks.avg_wait_seconds,
cur_ses_locks.username,
cur_ses_locks.status,
cur_ses_locks.event,
cur_ses_locks.machine,
cur_ses_locks.program,
cur_ses_locks.sql_id,
cur_ses_locks.prev_sql_id,
sysdate);
insert into dba_monitor.sqltext_kill_records (script) values (sqltext);
commit;
end if;
end loop;
END;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30126024/viewspace-2139809/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL:Innodb如何快速殺掉堵塞會話的思考MySql會話
- securecrt保持會話不會斷掉Securecrt會話
- RAC可以殺掉其他例項上的會話會話
- 怎麼殺掉特定的資料庫會話資料庫會話
- 11gRAC可以殺掉其他例項上的會話會話
- ?ORACLE會話超時Oracle會話
- CRISPR基因編輯技術會不會威脅人類自身?
- Redis鍵不會自動過期 - AblyRedis
- 【技巧】如何讓普通使用者可以殺掉自己使用者的會話會話
- 殺掉後臺執行的過程
- Oracle會話超時退出設定Oracle會話
- 死連線或不活動會話_DB處理機制會話
- 解決超過會話數問題會話
- 維爾納大學:研究發現人們網路上的朋友不會超過150人
- [轉]如何關閉一個不活動的或者空閒的 SSH 會話會話
- linux配置超時不操作自動退出登入TMOUTLinux
- ORACLE快速徹底Kill掉的會話Oracle會話
- 程式設計師界年度人口普查:6成以上開發者日工作超9小時,且從不運動程式設計師
- 日本自動駕駛新規:無人車路測時速不超20公里,須加手動裝置自動駕駛
- 自己動手2小時學會配置遊戲輔助遊戲
- ssl會話建立的過程(原理)是什麼?會話
- Oracle EBS 執行2小時自動停止,重起WEB 2小時又停止的問題解決.OracleWeb
- 如果沒有Java 人類就像不會說話的嬰兒Java
- linux批量殺掉程式linux批量殺掉程式Linux
- select 表的時候如果別的會話TRUNCATE DROP PURGE表後會是什麼結果會話
- cpu超頻和不超頻的區別是什麼 cpu超頻和不超頻哪個好
- 批量kill殺死某些會話session的plsql會話SessionSQL
- Oracle ERP 執行2小時自動停止,重起WEB 2小時又停止的問題解決.OracleWeb
- AI的殺手級應用會是一個“超級能幹的同事”!RAG會造就超級智慧麼?AI
- 快速殺掉Oracle的SessionOracleSession
- 殺掉鎖死的程式
- 找工作的考官出的題,限2個小時。做出來,留;不會,走人!
- 彭博社:超過30%英國人刪掉新冠追蹤AppAPP
- App被殺掉APP
- 每週工作不要超過40小時
- 幹掉APT的是人不是技術APT
- Laravel實現:待付款訂單,超48小時自動關閉Laravel
- 什麼是AI超自動化?AI