Kill locked sessions

Responsive image

Sometimes, when there’s many database sessions connected and depending on the user activity, locks can occur on a database. While this may only affect a few users, the problem is that with many sessions locked the server load will increase. And there’s a risk of crashing the whole database if this happens to many user sessions at the same time.

Of course, as a professional you implemented some sort of monitoring system to keep an eye on those metrics. But to be really efficient and prepared if you encounter a sudden peak of user sessions locked (right after deploying a new application version, for example) the most efficient method is to have a script ready specifically for this task.

The following script does just that. It kills all sessions locked for more than two minutes and outputs you the result. You can use it manually when needed, or schedule it in a cron job. Feel free to modify it to suit your needs.

#!/bin/sh

date

sqlplus -S login/password <<EOF

set pages 2000
set lines 140
set serveroutput on;

declare
nb_req number(30) := 0;
begin
for src in (select 'alter system kill session '''||sid||','||(select serial# from v\$session s where s.sid=l.sid)||'''' as req from v\$lock l where BLOCK = 1 and REQUEST = 0  and ctime > 120 order by CTIME desc)
loop
nb_req := nb_req+1;
dbms_output.put_line(src.req);
execute immediate src.req;
end loop;

dbms_output.put_line(nb_req||' sessions killed.');

end;
/
EOF
Written on March 20th, 2016 by Samy Gejzenblozen

Tags:


Social networks

You may also enjoy:

Forcing ASMM component to shrink

Forcing ASMM component to shrink

#oracle #database

Here's the way to force the shared pool to shrink dynamically. Documentation states that ASMM can only increase shared pool, and can't shrink. When the automatic shared memory management feature is enabled, the internal tuning algorithm tries to determine an optimal size for the shared pool based on the workload. It usually converges on this va... Read more

20 Mar 2019 - 2 minute read
How to extract Oracle user DDL

How to extract Oracle user DDL

#database #oracle

In some cases, you need to duplicate an Oracle user, along with all it's privileges. Doing this manually can be tedious. Hopefully, we can use the data dictionary to extract the data we need and dump it into a SQL file to modify and replay. Here's how to do it: ```sql set head off set pages 0 set long 9999999 spool user_script.sql SELECT DBMS... Read more

20 Feb 2018 - 1 minute read
Restart a hung Oracle database

Restart a hung Oracle database

#oracle

On rare occasions, mostly during a high server load peak or a process failure, an Oracle instance may not accept any connection. Either from regular users as well as SYSDBA. This situation is called a **hung database** and must be quickly resolved as the database isn't accessible for your users anymore. As you cannot connect to the hung database... Read more

20 May 2017 - 1 minute read