Oracle数据库表被锁了,如何解锁

Posted OkidoGreen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle数据库表被锁了,如何解锁相关的知识,希望对你有一定的参考价值。

急救SQL:查出所有被锁表的 sid 和 serial#,拼接成kill语句

select 'alter system kill session ''' || b.sid || ',' || b.serial# || '''immediate;'
from v$locked_object a,v$session b
where a.session_id = b.sid and a.SESSION_ID in
(select a.session_id
from v$locked_object f,dba_objects d
where d.object_id = f.object_id and d.OBJECT_NAME='T_CHANNEL_CONTACT') and b.username='MOBILEAPP'

alter system kill session '359,62263' immediate;

参考文章:

(765条消息) 1.oracle如何解决锁表_bunny_lhc的博客-CSDN博客_oracle 锁表https://blog.csdn.net/weixin_40068220/article/details/120643721

(765条消息) 怎么查看oracle数据库的表是否被锁 - CSDNhttps://www.csdn.net/tags/NtDaMg3sMjA4MjUtYmxvZwO0O0OO0O0O.htmlOracle查看锁表的SQL方法 - 动力节点Oracle查看锁表的SQL方法http://www.bjpowernode.com/hot/2211.html1. 首先查看数据库中哪些表被锁了,找到session ID:

使用sql:

select b.owner,b.object_name,a.session_id,a.locked_mode
from v$locked_object a,dba_objects b
where b.object_id = a.object_id;

OWNER :数据表的所有者用户

OBJECT_NAME: 被锁住的表名

SESSION_ID: 会话ID

LOCKED_MODE: 锁级别

锁级别分为6级:

1级锁有:Select

2级锁有:Select for update,Lock For Update,Lock Row Share

3级锁有:Insert, Update, Delete, Lock Row Exclusive

4级锁有:Create Index, Lock Share

5级锁有:Lock Share Row Exclusive 

6级锁有:Alter table, Drop table, Drop Index, Truncate table, Lock Exclusive

2.再执行该语句,查看会话id

select b.username,b.sid,b.serial#,logon_time 
from v$locked_object a,v$session b
where a.session_id = b.sid order by b.logon_time;

3.杀会话

alter system kill session 'sid,serial#';

例子:

alter system kill session '20,30153'; -----这个是我刚才杀死会话前的执行语句

如果有ora-00031错误,则在后面加immediate;

alter system kill session '20,30153' immediate;

-----------------------------------------------------------------------------------------------------------------------

锁表SQL

1.查看是否有锁表的sql

代码如下:

select 'blocker('||lb.sid||':'||sb.username||')-sql:'|| qb.sql_text blockers,
'waiter ('||lw.sid||':'||sw.username||')-sql:'|| qw.sql_text waiters
from v$lock lb,
v$lock lw,
v$session sb,
v$session sw,
v$sql qb,
v$sql qw
where lb.sid=sb.sid
and lw.sid=sw.sid
and sb.prev_sql_addr=qb.address
and sw.sql_address=qw.address
and lb.id1=lw.id1
and sw.lockwait is not null
and sb.lockwait is null
and lb.block=1 ;

2.查看被锁的表

代码如下:

select p.spid,a.serial#,c.object_name,b.session_id,b.oracle_username,b.os_user_name
from v$process p,v$session a, v$locked_object b,all_objects c
where p.addr=a.paddr and a.process=b.process and c.object_id=b.object_id ;

3.查看那个用户那个进程造成死锁,锁的级别

代码如下:

select b.owner,b.object_name,l.session_id,l.locked_mode fromv$locked_object l, dba_objects

4.查看连接的进程

代码如下:

SELECT sid, serial#, username, osuser FROMv$session;

5.查看是哪个session引起的

代码如下:

select b.username,b.sid,b.serial#,logon_time
from v$locked_object a,v$session b
where a.session_id = b.sid order by b.logon_time;

6.杀掉进程

代码如下:

alter system kill session 'sid,serial#';

通过上述相信大家对Oracle查看锁表的SQL方法已经有所了解,希望对大家能够有所帮助。

以上是关于Oracle数据库表被锁了,如何解锁的主要内容,如果未能解决你的问题,请参考以下文章

orcal数据库表被锁了怎么解锁??

oracle数据库表被锁了怎么解锁

oracle数据库表被锁了不能操作怎么办

MySQL锁表和解锁操作

oracle表被锁了怎么处理?

oracle查询锁表与解锁情况提供解决方案