KILL SESSION Procedure
i have created a procedure that I will check for open sessions and then kill them. I tried to compile this procedure using sqldeveloper and the output i got was Warning: execution completed with warning. package body Compiled. I then go and look for the package and i don't see it on the database. Here is my package and procedure...what am i doing wrong here? Is my syntax incorrect?
create or replace
package body pkg_sess_kill as
procedure sp_sess_kill is
--DECLARE
v_sid number;
v_serial# number;
v_username varchar2(30);
CURSOR checkuser IS
select sid,serial#,username
from v$session
where username IN ('CMSLOGGER',
'CMS2WIRE',
'SMS2WIRE');
BEGIN
OPEN checkuser;
FETCH checkuser into v_sid,v_serial#,v_username;
EXIT when checkuser%NOTFOUND;
IF v_username is not null THEN
ALTER SYSTEM KILL SESSION 'v_sid,v_serial#' IMMEDIATE;
END IF;
CLOSE checkuser;
END sp_sess_kill;
END pkg_sess_kill;
I understand that this is probably not the best way to address my problem. I am just starting off and still learning. I honestly just want my procedure to work and don't understand why it doesn't. I will look into other alternatives, but still would like to understand why it doesn't work.
Here is my procedure that will check for open sessions by particular usernames, and then kill the sessions if these usernames exist in the v$session view.
create or replace
procedure sp_sess_kill AS
--DECLARE
v_sid number;
v_serial# number;
v_username varchar2(30);
CURSOR checkuser IS
select sid,serial#,username
from v$session
where username IN ('USER1',
'USER2',
'USER3');
BEGIN
OPEN checkuser;
LOOP
FETCH checkuser into v_sid,v_serial#,v_username;
EXIT when checkuser%NOTFOUND;
IF v_username is not null THEN
execute immediate 'alter system kill session '||chr(39)||v_sid||','||v_serial#||chr(39);
END IF;
END LOOP;
CLOSE checkuser;
END sp_sess_kill;
The problem now is that i am getting the error msgs:
Error(8,15): PL/SQL:SQL Statement Ingored
Error(9,20): PL/SQL: ORA-00942: table or view does not exist.
This is odd because when i run the sql by itself outside the procedure block, i get a result set:
SID SERIAL# USERNAME
511 5105 USER1
516 7875 USER2
Please advise.
Similar Messages
-
Procedure for Killing sessions
Hi All,
Almost everyday we have requirement to kill user sessions for dev user, I'm thinking to create a procedure for this and grant to the users so that they can kill it by themself.
Below is the what I got from Ask Tom forum, however appreciate if someone can share few information if already imlemented in there environment
<quote>
create or replace procedure kill_session( p_sid in number,
p_serial# in number)
is
ignore pls_integer;
BEGIN
select count(*) into ignore
from V$session
where username = USER
and sid = p_sid
and serial# = p_serial# ;
if ( ignore = 1 )
then
execute immediate '
alter system kill session ''' ||
to_char(p_sid,'999999')||','||
to_char(p_serial#,'999999')||'''';
else
raise_application_error( -20001,
'You do not own session ''' ||
p_sid || ',' || p_serial# ||
end if;
END;/
grant execute on kill_session to <username>
</quote>
Regards,
shaanrp0428 wrote:
>
Instead of killing session with alter systemn kill session, better you opt for below two methods (still perform the same)
>
Please clarify what you mean. KILL and DISCONNECT do NOT perform the same.
From the SQL Language doc
http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_2013.htm
KILL is the nice one -
>
The KILL SESSION clause lets you mark a session as terminated, roll back ongoing transactions, release all session locks, and partially recover session resources
>
While DISCONNECT is the ogre
>
Use the DISCONNECT SESSION clause to disconnect the current session by destroying the dedicated server process (or virtual circuit if the connection was made by way of a Shared Sever).
>
The difference between the two is roughly analogous to the difference between SHUTDOWN IMMEDIATE and SHUTDOWN ABORT.
I agree that, for OPs use case DISCONNECT (with IMMEDIATE or POST TRANSACTION) may be better since it gets rid of things immediately while KILL can leave things hanging around for a while.From the same link:
DISCONNECT SESSION Clause:
The POST_TRANSACTION setting allows ongoing transactions to complete before the session is disconnected. If the session has no ongoing transactions, then this clause has the same effect described for as KILL SESSION.
The IMMEDIATE setting disconnects the session and recovers the entire session state immediately, without waiting for ongoing transactions to complete.
If you also specify POST_TRANSACTION and the session has ongoing transactions, then the IMMEDIATE keyword is ignored.
If you do not specify POST_TRANSACTION, or you specify POST_TRANSACTION but the session has no ongoing transactions, then this clause has the same effect as described for KILL SESSION IMMEDIATE.
basically the difference is not between DISCONNECT and KILL SESSION, the difference exists if you allow pending/ongoing transactions to finish(IMMEDIATE vs POST_TRANSACTION)
Edited by: Keilor on Jun 25, 2012 12:57 PM
Edited by: Keilor on Jun 25, 2012 1:39 PM -
Procedure that kill session....
Hello folks,
how can i write a procedure that provoque the sql statment alter system kill session'159,554' immediate;
i put it as it is in a pl/sql block but it gave error....
please advise........Something like this ?
SYS@db102 SQL> create or replace procedure killproc (
2 sid_in number,
3 serial_in number)
4 is
5 begin
6 execute immediate ('alter system kill session '||chr(39)||sid_in||','||serial_in||chr(39));
7* end;
SYS@db102 SQL> /
Procedure created.
SYS@db102 SQL> select username,sid,serial#,status from v$session
2 where username is not null;
USERNAME SID SERIAL# STATUS
SYS 145 124 ACTIVE
TEST 148 87 INACTIVE
SYS@db102 SQL> exec killproc(148,87);
PL/SQL procedure successfully completed.
SYS@db102 SQL> select username,sid,serial#,status from v$session
2 where username is not null;
USERNAME SID SERIAL# STATUS
SYS 145 124 ACTIVE
TEST 148 87 KILLED
SYS@db102 SQL> -
Urgent-how to run 'alter system kill session 'sid, serial#' in form 6i ?
I want to write a procedure in Form 6i so that user can kill the session by herself.
I know kill session sql is 'alter system kill session 'sid, serial#'', however, I fould that I can only run it it sql plus screen, how can I run it in Form or in Stored procedure?
Urgent....Please!try using Forms_ddl('alter system......'); in the forms. it will execute the dml statements in the form.
zaibi. -
ALTER SYSTEM KILL SESSION privilege
Hi All,
Is there any possibility to use this command from a without having DBA priviileges, what is the privilege to be grantd to the ordinary users to execute ALTER SYSTEM KILL SESSION... statement..
Thanks in advanceThe procedure proposed by Rusell would be more or less so:
SQL> create or replace procedure kill_session
2 ( v_sid number, v_serial number )
3 as
4 v_varchar2 varchar2(100);
5 begin
6 execute immediate 'ALTER SYSTEM KILL SESSION '''
7 || v_sid || ',' || v_serial || '''';
8 end;
9 /
Procedure created.
SQL> select username, sid, serial# from v$session;
USERNAME SID SERIAL#
147 5078
SYS 148 6161
151 6769
156 1
158 1
159 1
REPOS_OWNER 161 14502
163 1
164 1
165 1
166 1
USERNAME SID SERIAL#
167 1
168 1
169 1
170 1
15 rows selected.
SQL>
SQL> exec kill_session(161,14502);
PL/SQL procedure successfully completed.
SQL>Joel Pérez
http://otn.oracle.com/experts -
Kill session permission without alter system permission?
Syntax to kill session is
ALTER SYSTEM KILL SESSION 'session info';
Is there a way to allow a user to kill his/her own session without granting rights to SYSTEM? I don't want anyone to be able to drop the database, but they need to be able to kill their own session. I've looked everywhere...
Thanks in advance!Yes, assuming you are using standard definer's rights stored procedures. Only if you specify AUTHID CURRENT_USER when creating the stored procedure will you get an invoker's rights stored procedure, which would run with the privileges of the caller, not of the definer.
Justin -
Hi,
How can I batch kill session?
I have two related sessions in v$session by column client_info.
When os administrator kill the process, only kill the first session. I need to execute automatically the procedure:
psid number;
pserial# number;
session_killed EXCEPTION;
PRAGMA EXCEPTION_INIT(session_killed, -31);
begin
SELECT sid, serial# into psid, pserial# FROM v$session WHERE client_info = p_client_info;
EXECUTE IMMEDIATE 'alter system kill session ' || '''' || psid || ',' || pserial# || ''' immediate';
DBMS_OUTPUT.PUT_LINE('Killing user ' || psid || ', ' || pserial#);
EXCEPTION
WHEN session_killed THEN
DBMS_OUTPUT.PUT_LINE('The session ' || psid || ' has been marked to be killed');
END;
It is possible?
Help me, pls
BeatrizHi!
Pierre Forstmann, I have proven it, but I think that the trigger does not activate when kill becomes from the os.
-Sri, I'm executing a mapping of owb in production through a function.
The function generates one session in the data base and the call to mapping another one.
When mapping takes much, the production operators make kill process by means of a tool (controlM) that only suspends the first session.
But mapping continues executing itself because it runs under the other session.
I have related both sessions to identify them through the column client_info of v$session, now Its necessary automatically kill the second session, but I don't know how to do it.
thanks to all -
I'm curious as to if anybody has some insight to killed sessions. I have a procedure that looks like this:
procedure x
lv_result number;
lv_msg varchar2(4000);
for x in cursor
loop
lv_result := NULL;
lv_msg := NULL;
begin
execute immediate x.sql into lv_result;
exception
when others then
lv_msg := dbms_utility.format_error_stack;
lv_result = NULL;
end;
log_result; --autonomous transaction that inserts to table and commits;
end loop;
print_summary;
end x;What struck me as odd was that even though I'd kill the session while it was in the middle of processing rec 10 out of 30, it would still log the remaining 20 records with a "ORA-00028: your session has been killed" message. But it didn't print the results. So modified it a bit...
procedure x
SESSION_KILLED exception ;
pragma exception_init(SESSION_KILLED, -28);
lv_result number;
lv_msg varchar2(4000);
lv_session_killed boolean := FALSE;
for x in cursor
loop
lv_result := NULL;
lv_msg := NULL;
begin
execute immediate x.sql into lv_result;
exception
when session_killed then
lv_session_killed := TRUE;
lv_msg := dbms_utility.format_error_stack;
lv_result = NULL;
when others then
lv_msg := dbms_utility.format_error_stack;
lv_result = NULL;
end;
log_result; --autonomous transaction that inserts to table and commits;
exit when lv_session_killed;
end loop;
print_summary;
end x;...which effectively stopped the loop at the first record in the cursor at which the session was killed, and still didn't print the summary. But it's obvious that processing still "continues" in the background even though the session was killed. Does killing the session just terminate communications between the client and the DB, letting the procedure to continue on it's own? Is there any way to get DBMS_OUTPUT calls to work before the procedure actually dies?Database is 10.2.0.4.0
Here's a quickie example:
drop table tst_output;
create table tst_output (num number, msg varchar2(4000));
create or replace procedure p1
as
-- Make killed session named exception
SESSION_KILLED exception ;
pragma exception_init(SESSION_KILLED, -28);
-- Random query to generate string (a little slowly on my dev box).
lv_sql varchar2(4000) := 'select case when cnt is not null then ''SUCCESS!'' end as dummy_data ' ||
'from (select count(*) cnt from all_objects ' ||
'where last_ddl_time > (select min(last_analyzed) from all_tables))';
lv_return tst_output.msg%type; --result of execute immedaite
lv_session_killed boolean := FALSE; --stop processing if session killed.
procedure insert_rec (
p_num in tst_output.num%type,
p_msg in tst_output.msg%type)
as
pragma autonomous_transaction;
begin
insert into tst_output (num, msg)
values (p_num, p_msg);
commit;
end insert_rec;
procedure print_something
as
begin
for x in (select msg from tst_output)
loop
dbms_output.put_line(x.msg);
end loop;
end print_something;
begin
execute immediate 'truncate table tst_output';
for x in 1 .. 30 -- or however many loops it takes that you can jump to another window
loop
begin
execute immediate lv_sql into lv_return;
exception
when SESSION_KILLED then
lv_session_killed := TRUE;
lv_return := dbms_utility.format_error_stack;
dbms_output.put_line('Kill Time: ' || to_char(sysdate, 'dd-mon-yyyy'));
when OTHERS then
lv_return := dbms_utility.format_error_stack;
end;
insert_rec(x, lv_return);
--exit when lv_session_killed;
end loop;
print_something;
dbms_output.put_line('Finish Time: ' || to_char(sysdate, 'dd-mon-yyyy'));
end p1;Run this with a high enough loop count that you can jump to another window and kill that session...
ALTER SYSTEM KILL SESSION 'SID, SERIAL'If you log back in (disconnected from the session kill) and look a the tst_output table, you'll still see 1 record for every loop. If you uncomment the EXIT towards the end of the actual executable portion and do it again, the program will break from the loop when you kill the session.
So that means that the inner exception handler is catching the SESSION_KILLED_EXCEPTION, but
1) DBMS_OUTPUT in the exception is not displayed
2) It doesn't return to the normal flow after it is handled because the print at the bottom isn't displaying either.
The session running the procedure is killed (and disconnected), but doesn't get the DBMS_OUTPUT before it's disconnected...and the procedure continues processing till it's done? -
Killing Session : Form vs TOAD
1. How to distingusih between Form Session and Toad Session?
2. How to know if a session is being locked?
My problem is to kill locked session using a scheduler. Tx in advance.CREATE OR REPLACE PROCEDURE FIFAPPS.fs_p_kill_idle_session
IS
BEGIN
FOR pr IN (SELECT TO_NUMBER (value) waktu
FROM fifapps.FS_MST_PARAM a
WHERE MODULE_ID = 1
AND key = 'AUTO_KILL'
AND TO_NUMBER (value) > 0)
LOOP
FOR ss IN (SELECT SID, serial#,
ROUND (seconds_in_wait / 60, 2) minutes_wait, event,
machine, program, action, module
FROM v$session
WHERE service_name <> 'SYS$BACKGROUND'
AND event = 'SQL*Net message from client'
AND status <> 'KILLED'
AND upper(program) NOT LIKE '%TOAD%'
AND seconds_in_wait > pr.waktu)
LOOP
EXECUTE IMMEDIATE 'alter system kill session '''
|| ss.SID
|| ','
|| ss.serial#
|| '''';
END LOOP;
END LOOP;
END;
/I found the code above.. and some users said that the TOAD session is killed as well... -
Permissions for package owner for kill session?
What permissions does a package owner need to execute immediate 'alter system kill session' within a package?
Are you sure? It works for me on 10.2.0.1 (32 bit Windows)
SYS @ jcave102 Local> drop user bob cascade;
User dropped.
Elapsed: 00:00:11.25
SYS @ jcave102 Local> select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for 32-bit Windows: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
Elapsed: 00:00:00.10
SYS @ jcave102 Local> create user bob identified by bob default tablespace users;
User created.
Elapsed: 00:00:00.07
SYS @ jcave102 Local> grant create session, create procedure, alter system to bob;
Grant succeeded.
Elapsed: 00:00:00.01
SYS @ jcave102 Local> conn bob/bob
Connected.
1 create or replace procedure kill_session( p_sid in number, p_serial# in number )
2 as
3 begin
4 execute immediate 'alter system kill session ''' || p_sid || ',' || p_serial# || '''';
5* end;
BOB @ jcave102 > /
Procedure created.
Elapsed: 00:00:00.57Now, find a session to kill (using a user other than BOB who doesn't have permission to view the V$SESSION table) and call the procedure
BOB @ jcave102 Local> exec kill_session( 144, 115 );
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.00Justin -
In my application I want to kill session if any user
is idle for 2 minutes.
I am using oracle forms 6i and database oracle 9iYou have to use D2KWutil.pll
In when new form instance write the following code
declare
hWind PLS_INTEGER;
CheckTimer TIMER;
begin
hWind := get_window_property(FORMS_MDI_WINDOW,WINDOW_HANDLE);
Win_API_Session.Timeout_Start_Timer(hWind);
CheckTimer := Create_Timer('CheckTimeout',1000,repeat);
end;
and in when timer expired trigger write the following code
begin
if upper(get_application_property(TIMER_NAME)) = 'CHECKTIMEOUT' then
:timer.t1 := Win_api_session.Timeout_Get_Inactive_Time;
if :timer.t1 > 120 then -- timer is a block and t1 is text item (don't assign a canvas to it, make it invisible)
Win_API_Session.timeout_delete_timer;
exit_form (NO_VALIDATE);
end if;
end if;
end;
hope this helps !! -
Hi,
I kill a session with the commands:
select username, sid, serial#, status from v$session where username like 'RION%'
alter system kill session 'sid, serial#'
and if I give again the first select command at the status column appears KILLED. I want not to have KILLED in the status column.
Thank you,
Mihaelahi all,
As u know you are seeing the status of at v$session view as killed, I wanna confirm that this will exists in two senario.
When a Session is in inactive state and a kill command on that session is issued then the "status" column of v$session will be updated as killed as to indicate/mark as it is killed, and the "server " column will be updated as psuedo. when the user again try to connect to the session user will receive 0ra-00028 error and the entry will be removed from v$session.
Another senario is that the transaction is at its half way, either in commiting or at the roleback stage for that partucular session then also you can find that the v$table is updated as above.
more information, go through the documentation, at the Server Process handling section.
i hope it cleared your confussion, -
Oracle 10g r2
I killed a session after fetching the sid and serial from the v$session as under
select * from v$session where username='SAM';
alter system kill session '530,7420'
Now the status showed killed.
But again after some time one i query the v$session the status turn to inactive.
Why so?Yeah Madrid....
Thanks for that.. at the developer side.. they did get session killed.
But actually my doubt here was. that once i killed the session then i go the status as "killed" in the v$session
but less then a minute when i again query the v$session then i see the status aa inactive. This is what is not clear to me. -
RAR 5.3 - kill session of another user
how can RAR admin kill session of another user?
Hi Partha,
There is no way out to kill a user's session in RAR, nor in UME. The only place you can kill a user's session is in backend (R/3 side of a particular system) in T-code: SM04->select a particular session of any user-> end session.
Regards,
Gurugobinda -
To kill session in one schema from another schema
Hi Team,
I got a problem like a table from one of my schema has been locked. I am getting 'ORA-00054: resource busy and acquire with NOWAIT specified' error when trying to delete rows from that table or even when trying to truncate that table.
Let the table be 'T1' present in schema 'VIEW'
I tried to kill the session which is active for that schema by below query
select sid,serial#,status from v$session where username='VIEW' and STATUS = 'ACTIVE';
alter system kill session '681,2586';
But i couldn't do the above as i don't have DBA privilege for that. But i have DBA privilege for another schema let it be 'ADMIN'
Now how can i kill the session in schema 'VIEW' from schema 'ADMIN'
can any one get me solution.
Thanks in Advance
11081985I got a problem like a table from one of my schema has been locked. I am getting 'ORA-00054: resource busy and acquire with NOWAIT specified' error when trying to delete rows from that table or even when trying to truncate that table.
Before you do anything why don't you actually find out WHY that table has been locked.
You generally should NOT be killing sessions without knowing what is causing the problem to begin with.
Then you also need to determine if you should use KILL SESSION or instead use DISCONNECT SESSION and well as whether the use of IMMEDIATE is appropriate.
Each of those choices acts differently. Many people use KILL when they should really use DISCONNECT.
See DISCONNECT SESSION Clause and KILL SESSION Clause in the ALTER SESSION chapter of the SQL Language doc
http://docs.oracle.com/cd/E11882_01/server.112/e17118/statements_2014.htm#i2282145
Maybe you are looking for
-
How to hide columns that are getting added dynamically to UI Element 'Table
In SRM 7.0 while displaying a RFx, click on "responses and awards" button. In the response comparision tab once the user selects response number and clicks on "compare all responses". Item details table is displayed with fields item number,internal n
-
I think I'd read about how this is done but haven't been able to find the information. My two iMacs are connected through ethernet. I also have a macbook air which has a drive and all three macs are on the same wifi network. Maybe it can be done that
-
0I_CUFYE pulling incorrect year (ie. instead of pulling current year period
Hi , Since toady I am facign stragne error.. till yesterday the same query was showing restriction for 001.2010-012.2010 today it is showing 001.2011-012-2011 this variable 0I_CUFYE is dring me craz.. can anyone help... regards,.. rK
-
[ISE] What is the best Authorization rules sequence ?
Hello, like a FW set of rules, I think that ISE's authZ rules should also be ordered with care ? What are the best practices ? Most used first ? Guest, MAB and Webauth at the end ? Tell me... Any screencap is welcome Regards.
-
XEON PC OR TWO I7 WHIT A SHARE DISK CONFIG.
These is simply. It is possible to have two pc's let say: 2X I7 930 WITH a share and grate raid congif. So you can use 1 pc for importing and extra task and the other for CS5 programs, that way we reduce the possibility of unespect crashes.????