首页 > 数据库 >【检查ORACLE阻塞】如果阻塞超过N秒则发短信报警并KILL进程

【检查ORACLE阻塞】如果阻塞超过N秒则发短信报警并KILL进程

时间:2022-11-16 17:24:40浏览次数:48  
标签:clinicdb SET int phones 阻塞 KILL ORACLE DECLARE SELECT

检查oracle阻塞,如果阻塞超过N秒则发短信报警
阻塞超过分钟后自动kill进程
检查oracle分布式事务预留锁,回滚事务

/*** 注意:链接服务器clinicdb要设置RPC和RPC Out 为true,否则无法自动解锁***/
CREATE PROCEDURE [dbo].[pCheckOracleLock]
AS
SET  NOCOUNT ON
DECLARE @phones varchar(500)
DECLARE @i int
DECLARE @sid  int
DECLARE @serial  int
DECLARE @ctime  int

DECLARE @txt varchar(4000)
--Declare @killTxt varchar(100)

SELECT IDENTITY(int ,1,1) ID,* INTO #tmp FROM OPENQUERY(clinicdb,'select * from apps.Locks where ctime>100')

IF EXISTS(SELECT * FROM #tmp)
BEGIN	
	SELECT @phones=replace(value,' ','') FROM OPENQUERY(clinicdb,'select value from core.parameterValue where name =''DutyPhoneNums'' ')
	SET @phones=replace(@phones,',',',')

	IF @phones IS NULL OR @phones='' 
		RAISERROR('pCheckOracleLock存储过程执行错误:系统参数DutyPhoneNums未设置',18,1)
		
	SET @i=1
	SET @txt=''
	WHILE @i<=(SELECT max(ID) FROM #tmp)
	BEGIN		
		SELECT @txt='Oracle出现阻塞:SID='+ltrim(SID)+',MACHINE='+isnull(MACHINE,'')+',PROGRAM='+isnull(PROGRAM,'')+',obj='+lower(isnull(OWNER,'')+'.'+isnull(OBJECT_NAME,''))
			+',已阻塞'+isnull(ltrim(ctime),'')+'秒'
			+CASE WHEN ctime IS NOT NULL AND ctime>600 THEN ',【将立即终止该进程!!!】' ELSE '' END
			+',SQL:'+char(13)+char(10)+LEFT(isnull(SQL_TEXT,''),150)
		FROM #tmp WHERE ID=@i
		
		INSERT INTO [sy30smoke].[JX_T20_CRM].DBO.tSMSSend(content,mobiletelePhone,typeflag,statusflag,appenddatetime)
			SELECT @txt, t.s, 16 AS typeflag, 0 AS statusflag, getdate() AS appenddatetime
			FROM dbo.[fnSplitString](@phones,',') t
		 
		SET @i=@i+1
	END
	
	SET @i=1
	WHILE @i<=(SELECT max(ID) FROM #tmp)
	BEGIN
		SELECT @ctime=ctime, @serial=serial, @sid=[sid] 
		FROM #tmp WHERE ID=@i
				 
		IF @ctime>600
		BEGIN
			EXEC('begin
			pKillSession(?,?);
			commit;
			end;', @sid,@serial ) at clinicdb
		END		 
		 
		SET @i=@i+1
	END
END

--分布式事务预留锁
SELECT IDENTITY(int ,1,1) ID,* INTO #pending
FROM OPENQUERY(clinicdb,'select LOCAL_TRAN_ID, to_char(Fail_Time,''yyyy-mm-dd HH24:mi:ss'') as Fail_Time, Host from dba_2pc_pending where fail_time>sysdate-1 and fail_time is not null and state=''prepared'' ')

IF EXISTS(SELECT * FROM #pending)
BEGIN
	SELECT @phones=replace(value,' ','') FROM OPENQUERY(clinicdb,'select value from core.parameterValue where name =''DutyPhoneNums'' ')
	SET @phones=replace(@phones,',',',')

	IF @phones IS NULL OR @phones='' 
		RAISERROR('pCheckOracleLock存储过程执行错误:系统参数DutyPhoneNums未设置',18,1)
	
	DECLARE @dt datetime
	SET @dt=dateadd(second,-600,getdate()) 
	
	SET @i=1
	SET @txt=''
	WHILE @i<=(SELECT max(ID) FROM #pending)
	BEGIN
		SELECT @txt='Oracle出现分布式事务预留锁:LOCAL_TRAN_ID='+ltrim(LOCAL_TRAN_ID)+',Fail_Time='+isnull(Fail_Time,'')+',Host='+isnull(Host,'')+char(13)+char(10)		
			+CASE WHEN Fail_Time IS NOT NULL AND Fail_Time<@dt 
				  THEN ',【将立即回滚该事务!!!】' 
				  ELSE '执行下述sql可解锁:'+char(13)+char(10)
						+'Rollback force '''+ltrim(LOCAL_TRAN_ID)+''';'+char(13)+char(10)
						+'execute dbms_transaction.purge_lost_db_entry('''+ltrim(LOCAL_TRAN_ID)+''');'+char(13)+char(10)
						+'commit;'
			 END
		FROM #pending WHERE ID=@i
		
		INSERT INTO [sy30smoke].[JX_T20_CRM].DBO.tSMSSend(content,mobiletelePhone,typeflag,statusflag,appenddatetime)
			SELECT @txt, t.s, 16 AS typeflag, 0 AS statusflag, getdate() AS appenddatetime
			FROM dbo.[fnSplitString](@phones,',') t
		 
		SET @i=@i+1
	END
	
	SET @i=1
	DECLARE @Fail_Time datetime
	DECLARE @LOCAL_TRAN_ID varchar(100)
	WHILE @i<=(SELECT max(ID) FROM #pending)
	BEGIN
		SELECT @Fail_Time=Fail_Time, @LOCAL_TRAN_ID=''''+ltrim(LOCAL_TRAN_ID)+''''
		FROM #pending WHERE ID=@i
		
		IF @Fail_Time<@dt
		BEGIN
			EXEC('begin
			  pRollbackTransaction(?);
			  commit;
			  end;', @LOCAL_TRAN_ID ) at clinicdb
		END		 
		 
		SET @i=@i+1
	END
END
	
DROP TABLE #tmp
DROP TABLE #pending

 

标签:clinicdb,SET,int,phones,阻塞,KILL,ORACLE,DECLARE,SELECT
From: https://www.cnblogs.com/chriskwok/p/16896600.html

相关文章

  • Oracle创建表空间
    创建表空间并定义路径:createtablespaceLIANGFAXIANJIEdatafile'D:/app/PC/oradata/orcl/LIANGFAXIANJIE.dbf'size500mautoextendonnext50mmaxsize20480mextentm......
  • Oracle11g RAC集群启动流程
    一、集群与资源启动顺序启动流程步骤层次梳理第一层:OHASD启动:cssdagent-负责启动CSSD的Agent。orarootagent-负责启动所有root用户下的ohasd资源的A......
  • Oracle 的 Redo log 日志
    Oracle的数据库日志称为redolog,所有数据改变都记录redolog,可以用于修复受损的数据库,Redo日志是分组的,默认是3组,Redo日志是轮流使用的,一个redolog满了,LGWR会切换到下......
  • oracle 会话查询
    需要管理员用户下执行(sys/sysdba)--先查锁select*fromv$lockwherelmode>0andtypein('TM','TX');--查用户名select*fromv$sessionwhereusername='P......
  • 限制用户只能在特定时间段登录 oracle
    需求:只允许一些用户在特定时间内登录实现方式:通过触发器去实现(对具有dba权限的用户不适用),触发器代码如下CREATEORREPLACETRIGGERlimit_connectionAFTERLO......
  • 080_阻塞队列 BlockingQueue
    目录简介演示代码抛出异常add()添加元素队列已满时抛出异常remove()移除元素为空时抛出异常有返回值,不抛出异常offer()添加元素队列已满时返回false不抛异常poll()移除......
  • CentOS7_Oracle12C 安装非容器数据库
    Oracle12C安装非容器数据库1.安装环境准备1.1条件准备Oracle安装包:linuxx64_12201_database.zipSSH远程工具:MobaxVMware虚拟机:centos7.6(内存:2G,Swap:3G磁......
  • [Oracle] 11gR2 GI Node May not Join the Cluster After Private Network is Functio
    11gR2GINodeMaynotJointheClusterAfterPrivateNetworkisFunctionalAfterEvictionduetoPrivateNetworkProblem(文档ID1479380.1)InthisDocument......
  • 设置oracle19c开机自启动
    1、以root身份登录系统,修改oratab,如下图所示:vi/etc/oratab进入vi编辑器后,找到“ORCL:/u01/app/oracle/product/19.3.0/db_1::N”将文件最后面的N,修改为Y,如下:......
  • 解决:ORA-01034: ORACLE not available问题
    1先看oracle的监听和oracle的服务是否都启动了。启动oracle监听:cmd的命令行窗口下,输入lsnrctlstart,回车即启动监听。2查看oracle的sid叫什么,比如创建数据库的时候,实例名......