【ORACLE】查看死锁进程并结束死锁的脚本

--共享锁:Share;排他锁:Exclusive;行共享锁:Row-S;行排他锁:Row-X
select   V$SESSION.sid,v$session.SERIAL#,v$process.spid,
  rtrim(object_type)   object_type,rtrim(owner)   ||   ‘.‘   ||   object_name   object_name,
  decode(lmode,       0,   ‘None‘,
  1,   ‘Null‘,
  2,   ‘Row-S‘,
  3,   ‘Row-X‘,
  4,   ‘Share‘,
  5,   ‘S/Row-X‘,
  6,   ‘Exclusive‘, ‘Unknown‘)   LockMode,
  decode(request,   0,   ‘None‘,
  1,   ‘Null‘,
  2,   ‘Row-S‘,
  3,   ‘Row-X‘,
  4,   ‘Share‘,
  5,   ‘S/Row-X‘,
  6,   ‘Exclusive‘,   ‘Unknown‘)   RequestMode
  ,ctime,   block   b,
  v$session.username,MACHINE,MODULE,ACTION,
  decode(A.type,
  ‘MR‘,   ‘Media   Recovery‘,
  ‘RT‘,‘Redo   Thread‘,
  ‘UN‘,‘User   Name‘,
  ‘TX‘,   ‘Transaction‘,
  ‘TM‘,   ‘DML‘,
  ‘UL‘,   ‘PL/SQL   User   Lock‘,
  ‘DX‘,   ‘Distributed   Xaction‘,
  ‘CF‘,   ‘Control   File‘,
  ‘IS‘,   ‘Instance   State‘,
  ‘FS‘,   ‘File   Set‘,
  ‘IR‘,   ‘Instance   Recovery‘,
  ‘ST‘,   ‘Disk   Space   Transaction‘,
  ‘TS‘,   ‘Temp   Segment‘,
  ‘IV‘,   ‘Library   Cache   Invalida-tion‘,
  ‘LS‘,   ‘Log   Start   or   Switch‘,
  ‘RW‘,   ‘Row   Wait‘,
  ‘SQ‘,   ‘Sequence   Number‘,
  ‘TE‘,   ‘Extend   Table‘,
  ‘TT‘,   ‘Temp   Table‘,
  ‘Unknown‘)   LockType
  from   (SELECT   *   FROM   V$LOCK)   A,   all_objects, V$SESSION, v$process
  where   A.sid   >   6
  and   object_name<>‘OBJ$‘
  and   A.id1   =   all_objects.object_id
  and   A.sid=v$session.sid
  and   v$process.addr=v$session.paddr; 

--查看锁表进程SQL语句1:
select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo.locked_mode
from v$locked_object lo, dba_objects ao, v$session sess
where ao.object_id = lo.object_id and lo.session_id = sess.sid; 

--杀掉锁表进程:
--如有記錄則表示有lock,記錄下SID, serial# ,將記錄的ID替換下面的1143,24155,即可解除LOCK
alter system kill session ‘1143,24155‘;
SELECT Saddr, Sid, Serial#, Paddr, Username, Status FROM V$session t WHERE t.SID = ‘1143‘;

--查看机器锁定代码对象
select b.sid,b.serial#,b.machine,b.terminal,b.program,b.process,b.status from v$lock a , v$session b
where a.SID = b.SID and b.MACHINE=‘guoshaoyue-5040‘;

--用这个可以查(可以查看哪台机器哪个用户锁了记录, 其中command是用来杀掉锁住记录的session ):
select s.sid,s.machine,o.object_name,l.oracle_username,l.locked_mode, ‘ALTER  SYSTEM  KILL  SESSION  ‘‘‘||s.sid||‘,  ‘||s.serial#||‘‘‘;‘ Command
from  v$locked_object  l,v$session  s,all_objects  o  where  l.session_id=s.sid  and  l.object_id=o.object_id; 

--同样也是通过写sql从数据字典里查出来。
-- SELECT-SQL1 功能:检查被加锁的对象
select obj.OWNER||‘.‘||obj.OBJECT_NAME as OBJ_NAME, --// 对象名称(已经被锁住)
   obj.SUBOBJECT_NAME as SUBOBJ_NAME,     --// 子对象名称(已经被锁住)
   obj.OBJECT_ID as OBJ_ID,        --// 对象ID
   obj.OBJECT_TYPE as OBJ_TYPE,       --// 对象类型
   lock_obj.SESSION_ID as SESSION_ID,     --// 会话SESSION_ID
   lock_obj.ORACLE_USERNAME as ORA_USERNAME,  --// ORACLE系统用户名称
   lock_obj.OS_USER_NAME as OS_USERNAME,    --// 操作系统用户名称
   lock_obj.PROCESS as PROCESS       --// 进程编号
from
  ( select * from all_objects where object_id in (select object_id from v$locked_object)) obj,
  v$locked_object lock_obj
where obj.object_id=lock_obj.object_id; 

-- SELECT-SQL2 功能:检查被加锁的对象以及加锁的会话信息
-- ALTER-SQL1 功能:杀死会话(SESSION_ID,SERIAL#),可以手工解除锁
-- 请手工修改SESSION_ID,SERIAL#为相应值
-- 注意:本功能慎重使用,有一定的破坏性,该SQL可以断开客户机和服务器的连接
-- ALTER SYSTEM KILL SESSION ‘SESSION_ID,SERIAL#‘;
select LOCK_INFO.OWNER||‘.‘||LOCK_INFO.OBJ_NAME as OBJ_NAME, --// 对象名称(已经被锁住)
   LOCK_INFO.SUBOBJ_NAME as SUBOBJ_NAME,       --// 子对象名称(已经被锁住)
   SESS_INFO.MACHINE as MACHINE,         --// 机器名称
   LOCK_INFO.SESSION_ID as SESSION_ID,       --// 会话SESSION_ID
   SESS_INFO.SERIAL# as SERIAL#,         --// 会话SERIAL#
   LOCK_INFO.ORA_USERNAME as ORA_USERNAME,      --// ORACLE系统用户名称
   LOCK_INFO.OS_USERNAME as OS_USERNAME,       --// 操作系统用户名称
   LOCK_INFO.PROCESS as PROCESS,         --// 进程编号
   LOCK_INFO.OBJ_ID as OBJ_ID,          --// 对象ID
   LOCK_INFO.OBJ_TYPE as OBJ_TYPE,         --// 对象类型
   SESS_INFO.LOGON_TIME as LOGON_TIME,       --// 登录时间
   SESS_INFO.PROGRAM  as PROGRAM,         --// 程序名称
   SESS_INFO.STATUS as STATUS,          --// 会话状态
   SESS_INFO.LOCKWAIT as LOCKWAIT,         --// 等待锁
   SESS_INFO.ACTION as ACTION,          --// 动作
   SESS_INFO.CLIENT_INFO as CLIENT_INFO       --// 客户信息 

from
 (
  select obj.OWNER as OWNER,
     obj.OBJECT_NAME as OBJ_NAME,
     obj.SUBOBJECT_NAME as SUBOBJ_NAME,
     obj.OBJECT_ID as OBJ_ID,
     obj.OBJECT_TYPE as OBJ_TYPE,
     lock_obj.SESSION_ID as SESSION_ID,
     lock_obj.ORACLE_USERNAME as ORA_USERNAME,
     lock_obj.OS_USER_NAME as OS_USERNAME,
     lock_obj.PROCESS as PROCESS
  from
   ( select * from all_objects where object_id in (select object_id from v$locked_object)) obj,
   v$locked_object lock_obj
  where obj.object_id=lock_obj.object_id
 )  LOCK_INFO,
 (
  select SID,
     SERIAL#,
     LOCKWAIT,
     STATUS,
     PROGRAM,
     ACTION,
     CLIENT_INFO,
     LOGON_TIME,
     MACHINE
  from v$session
 ) SESS_INFO
where LOCK_INFO.SESSION_ID=SESS_INFO.SID ;

原文地址:https://www.cnblogs.com/yaoyangding/p/12048110.html

时间: 2024-10-03 19:28:28

【ORACLE】查看死锁进程并结束死锁的脚本的相关文章

查看oracle死锁进程并结束死锁

查看锁表进程SQL语句1: select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo.locked_mode from v$locked_object lo, dba_objects ao, v$session sess where ao.object_id = lo.object_id and lo.session_id = sess.sid; 查看锁表进程SQL语句2: sel

【转】查看oracle死锁进程并结束死锁

--查看锁表进程SQL语句1: select sess.sid, sess.serial#, lo.oracle_username, lo.os_user_name, ao.object_name, lo.locked_mode from v$locked_object lo, dba_objects ao, v$session sess where ao.object_id = lo.object_id and lo.session_id = sess.sid; --查看锁表进程SQL语句2:

Windows下查看8080进程及结束进程命令

Windows下查看进程及结束进程命令 1)查看占用8080端口的进程号 >netstat –aon | findstr “8080” 结果:TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       3500 可知,进程号为3500的进程占用了8080端口 可以使用命令 tasklist | findstr “3500”进一步查看3500进程的具体信息. > tasklist | findstr “3500” 结果

读取oracle页面或者进程卡住不动(死锁)

oracle最坑爹的地方:你insert   update  delete之后  或者kill死锁的时候记得一定要提交事务不然就是死锁卡在那里了 记住  kill死锁也是要提交事务的 select * from v$session where sid in (select session_id from v$locked_object) SELECT s.username,l.OBJECT_ID,l.SESSION_ID,s.SERIAL#,l.ORACLE_USERNAME,l.OS_USER

sql server 查看表的死锁和Kill 死锁进程

查询出来 select        request_session_id spid,       OBJECT_NAME(resource_associated_entity_id) tableName    from        sys.dm_tran_locks   where        resource_type='OBJECT 杀死死锁进程 kill spid 另: exec master.dbo.sp_who_lock --查看当前死锁进程 exec master.dbo.p_

Linux内核调试技术——进程D状态死锁检测

Linux的进程存在多种状态,如TASK_RUNNING的运行态.EXIT_DEAD的停止态和TASK_INTERRUPTIBLE的接收信号的等待状态等等(可在include/linux/sched.h中查看).其中有一种状态等待为TASK_UNINTERRUPTIBLE,称为D状态,该种状态下进程不接收信号,只能通过wake_up唤醒.处于这种状态的情况有很多,例如mutex锁就可能会设置进程于该状态,有时候进程在等待某种IO资源就绪时(wait_event机制)会设置进程进入该状态.一般情况

事务(进程 ID )与另一个进程已被死锁在 lock 资源上,且该事务已被选作死锁牺牲品。请重新运行该事务

其实所有的死锁最深层的原因就是一个:资源竞争 表现一:    一个用户A 访问表A(锁住了表A),然后又访问表B    另一个用户B 访问表B(锁住了表B),然后企图访问表A 这时用户A由于用户B已经锁住表B,它必须等待用户B释放表B,才能继续,好了他老人家就只好老老实实在这等了    同样用户B要等用户A释放表A才能继续这就死锁了解决方法:    这种死锁是由于你的程序的BUG产生的,除了调整你的程序的逻辑别无他法    仔细分析你程序的逻辑,    1:尽量避免同时锁定两个资源    2:

查看数据库里阻塞和死锁情况

/*********************************** //删除 死锁 存储过程 ***************************************/ if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_who_lock]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[sp_who_l

Linux下使用ps命令来查看Oracle相关的进程

Linux下可以使用ps命令来查看Oracle相关的进程 Oracle Listener 这个命令会列出Oracle Net Listener的进程 [[email protected] www.linuxidc.com ~]$ ps -ef | grep tnslsnr oracle    1999 29986  0 09:47 pts/2    00:00:00 grep tnslsnr oracle    3647     1  0 Jul03 ?        00:00:34 /u01