Articles Comments

Oracle DBA & All IT » Database, DBA, DBA, Oracle, Oracle Tips, รวม Script ต่างๆ » ตัวอย่าง Program สำหรับให้ user Kill ตัวเอง

ตัวอย่าง Program สำหรับให้ user Kill ตัวเอง

         หลายครั้งที่ User ต้องขัดใจและเสียเวลา หรือว่าเกรงใจในการจะให้ DBA ทำการ Kill Session ที่รัน เนื่องด้วยเหตุผลใดทั้งปวงก็ตาม DBA อย่างเราเนื่องด้วยสิทธิ์การ Kill session นั้นจะต้องได้ Privilege อันสูงส่ง นั่นคือ ALTER SYSTEM ซึ่งถ้าได้ไป ฐานข้อมูลอันเป็นที่รักของเราคงต้องอยู่ในอันตรายอย่างแน่นอน
 

         แต่อย่างไรก็ตามทุกปัญหามีหนทางแก้ไขเสมอ โดยการที่เราสามารถสร้าง Porcedure ขึ้นมาครอบ และกำหนดสิทธิ์ให้ owner ของ Procedure ต้องได้สิทธิ์ ALTER SYSTEM และ grant execute ให้กับ user or role ที่เราต้องการให้มีสิทธิ์ Kill เพียงเท่านี้ User ทั่วไปก็จะมีสิทธิ์ Kill ตัวเอง แต่ไม่มีสิทธืที่จะรันคำสั่ง ALTER SYSTEM อย่างอื่นในระบบได้

SHOWME: สำหรับแสดง SID,SERIAL#

CREATE OR REPLACE procedure scott.showme as
   v1 varchar2(50);
   v2 number;
   v3 number;
   v4 varchar2(50);
   v5 varchar2(50);
   v6 varchar2(50);

   CURSOR c1 IS select s.username,s.sid,s.serial#,s.status,s.program,to_char(s.logon_time, 'dd/mm/rrrr hh24:mi:ss') time_logon into v1,v2,v3,v4,v5,v6 from V$SESSION s , V$PROCESS p where s.username = (select user  from dual)  and s.paddr = p.addr (+) order by s.username, s.logon_time;

begin
   open c1;
      dbms_output.put_line('USERNAME        SID            SERIAL#        STATUS         PROGRAM       TIME_LOGON');  
      dbms_output.put_line('————————————————————————————-');  
  LOOP
      Fetch c1 into v1,v2,v3,v4,v5,v6;
      Exit when c1%NOTFOUND;
      dbms_output.put_line(v1||'         '||v2||'         '||v3||'         '||v4||'         '||v5||'         '||v6);
  —    exit;
   END LOOP;
   Close c1;
END;
/

ตัวอย่างการรัน SHOWME:

SQL> execute showme
USERNAME        SID            SERIAL#        STATUS         PROGRAM       TIME_LOGON
————————————————————————————-
TOMMY         211         43380         ACTIVE         sqlplusw.exe         30/06/2011 20:36:11
TOMMY         662         4526         INACTIVE         sqlplusw.exe         30/06/2011 20:38:03

 

KILLME: สำหรับ Kill session  โดยรับ พารามิเตอร์ SID,SERIAL# ซึ่งดูได้จาก SHOWME

CREATE OR REPLACE procedure scott.killme (vsid number,vserial number) as
   vname varchar2(20);
   vcheck varchar2(20);
   exec1 varchar2(255);
   exec2 varchar2(255);
   exec3 varchar2(255);
begin
   begin
    SELECT username  into vcheck from V$SESSION where sid=vsid and serial#=vserial;
—    dbms_output.put_line('Vcheck OK:'||vcheck);
   EXCEPTION  WHEN OTHERS THEN        
         dbms_output.put_line('————————————————————-');
  dbms_output.put_line('  No SID '||'&'||' SERIAL in SYSTEM :: Please verify again!!');
         dbms_output.put_line('————————————————————-'); 
   END;

   begin
   select user into vname from dual;
—   dbms_output.put_line('Vname OK:'||vname);
   EXCEPTION  WHEN OTHERS THEN
         dbms_output.put_line('————————');
  dbms_output.put_line('  Vname Error:'||vname); 
         dbms_output.put_line('————————'); 
   END;
   begin
        if vcheck=vname then
     —      dbms_output.put_line('sid serial: '|| vsid||' '||vserial);
         exec3 :='ALTER SYSTEM KILL SESSION '||chr(39)||vsid||','||vserial||chr(39)||' immediate';
         EXECUTE IMMEDIATE exec3;
         dbms_output.put_line('######################################################################');
         dbms_output.put_line('Kill Session Sid:'||vsid||' & '||'Serial#:'||vserial||' Successfully!!');
         dbms_output.put_line('######################################################################');
        ELSE
             dbms_output.put_line('####################################');
         dbms_output.put_line(' Kill Session is Not Successfully !!');
         dbms_output.put_line('####################################');
        end if;
   EXCEPTION  WHEN OTHERS THEN
         dbms_output.put_line('——————————-');
  dbms_output.put_line(' Cannot Kill Current Session'); 
         dbms_output.put_line('——————————-'); 
   END;

END;
/

ตัวอย่างการรัน KILLME:

SQL> execute killme(662,4526);
######################################################################
Kill Session Sid:662 & Serial#:4526 Successfully!!
######################################################################

PL/SQL procedure successfully completed.

 PRIVILEGE ที่ user SCOTT ต้องมี:
– grant alter system to scott;
– grant select on v_$process to scott;
– grant select on v_$session to scott;

สร้าง Synonym เพื่อเวลาเรียกใช้ไม่ต้องปะ owner:
Create public synonym showme for scott.showme;
Create public synonym killme for scott.killme;

สุดท้าย สร้าง Role ใหม่ แล้ว grant role ให้กับ user ที่เราต้องการให้สิทธิ์:
create role killsession;
grant execute on showme to killsession;
grant execute on killme to killsession;

เป็นอันจบ…เพื่อนๆสามารถที่นำไปประยุกต์ใช้กับอย่างอื่นได้..หวังว่าบทความมีจะมีประโยชน์กับเพื่อนๆไม่มากก็น้อยจ้า..

 

.. Preview : 10563

Related Search:

  • synonym คือ
  • ตัวอย่าง oracle

Written by

บอกเล่าสิ่งที่พบเจอมาในการทำงาน ประสบการณ์การทำงานด้าน DBA ถ่ายทอดกันด้วยภาษาง่ายๆ บ้านๆ " ทุกอย่างไม่อยาก แต่... แค่ตั้งใจไม่พอ ต้องลงมือทำ และทำ GoodLuck " Fanpage: www.facebook.com/DBAor .. "Oracle Database Consultant " ..

Filed under: Database, DBA, DBA, Oracle, Oracle Tips, รวม Script ต่างๆ · Tags: , , , , , , , , , , ,

  • Pingback: ตัวอย่าง Program สำหรับให้ user Kill ตัวเอง | Oracle in Thai | Oracle in Thai | Oracle User Group in Thailand |()

    • And I thouhgt I was the sensible one. Thanks for setting me straight.

    • I found just what I was needed, and it was enetriatning!

  • lithium ion laptop batteries are very good specially those 6 cell battery array.,

    • I love raiendg these articles because they’re short but informative.

    • Good point. I hadn’t thhogut about it quite that way. 🙂

  • I’ve read through 20 different blogs on this and yours is easily the best. Thanks!

    • What an aswmeoe way to explain this-now I know everything!

    • If you’re loonikg to buy these articles make it way easier.

  • I found out your website page from google and it truly is pretty informative. Thank You for writing this kind of an informative blog post.

    • Articles like these put the consmeur in the driver seat-very important.

    • Slam dunkin like Shaqulile O’Neal, if he wrote informative articles.

  • Some times its a pain in the ass to read what people wrote but this website is rattling user genial ! .

  • I have been so bweildreed in the past but now it all makes sense!

  • Not only is he defending an asshole (West), but he didnt even get his history right. Gallileo was actually invited by the Pope himself to lecture on his new theories. Heretics didnt receive such a warm invitation from the pontiff in the middle ages. It was only after Gallileo repeatedly insulted the Pope that the Pope, in a childish fit, effectively proscribed Gallileo. In other words, it was personal, not about science or religion.