清理数据库连接线程

简介:
复制代码
 
CREATE proc usp_clr_Session
as
begin
DECLARE @spid INT , @strSql VARCHAR ( 200 ), @pid int

if object_id ( ' tempdb..#temp ' ) is not null
drop table # temp
create table # temp
(
id
int identity ( 1 , 1 ) not null
,Proc_info
varchar ( 8000 ) null
)
-- insert data into temp table
insert into # temp (Proc_info)
exec master..xp_cmdshell ' tasklist /v /fi "imagename eq clinkivr.exe" '

-- delete the wrong records
delete from # temp
where Proc_info is null or Proc_info like ' %==========% ' or ID = 2

select @pid = isnull ( max (dbo.F_split_V2(Proc_info, 2 )) , - 1 )
from # temp

print ( @pid )

DECLARE clr_sp_cursor CURSOR FOR
SELECT SPID
FROM master..sysprocesses
WHERE cmd = ' AWAITING COMMAND ' AND dbid > 4 AND status = ' sleeping ' and hostprocess = @pid

OPEN clr_sp_cursor;

FETCH NEXT FROM clr_sp_cursor
INTO @spid ;

WHILE @@FETCH_STATUS = 0
BEGIN
SET @strSql = ' kill ' + RTRIM ( @spid )
PRINT ( @strsql )
EXEC ( @strsql )
FETCH NEXT FROM clr_sp_cursor
INTO @spid ;
END
CLOSE clr_sp_cursor;
DEALLOCATE clr_sp_cursor;

end
GO
复制代码
 
  
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

/*
select * FROM master..sysprocesses where hostprocess = 3368 order by cpu desc

master..xp_cmdshell 'tasklist /v'
*/

-- ALTER a splite function
Create function F_split_V2( @S nvarchar ( 4000 ), @i int )
returns nvarchar ( 100 )
as
begin
if @i = 1 -- Image_name
begin
if patindex ( ' %.exe% ' , @s ) = 0
set @s = rtrim ( left ( @s , patindex ( ' %[0-9]% ' , @s ) - 1 ))
else
set @s = rtrim ( left ( @s , patindex ( ' %.exe% ' , @s ) + 4 ))
end
else if @i = 2 -- Pid
begin
if patindex ( ' %.exe% ' , @s ) = 0
set @s = stuff ( @s , 1 , patindex ( ' %[0-9]% ' , @s ) - 1 , '' )
else
set @s = ltrim ( substring ( @s , patindex ( ' %.exe% ' , @s ) + 4 , len ( @s )))

select @s = rtrim ( left ( @s , patindex ( ' %[^0-9]% ' , @s ) - 1 ))
end
else if @i = 3 -- Session_name
begin
if patindex ( ' %.exe% ' , @s ) = 0
set @s = stuff ( @s , 1 , patindex ( ' %[0-9]% ' , @s ) - 1 , '' )
else
set @s = ltrim ( substring ( @s , patindex ( ' %.exe% ' , @s ) + 4 , len ( @s )))
select @s = stuff ( @s , 1 , patindex ( ' %[^0-9]% ' , @s ) - 1 , '' ),
@s = rtrim ( left ( @s , patindex ( ' %[0-9]% ' , @s ) - 1 ))
end
else if @i = 4 -- Session#
begin
if patindex ( ' %.exe% ' , @s ) = 0
set @s = stuff ( @s , 1 , patindex ( ' %[0-9]% ' , @s ) - 1 , '' )
else
set @s = ltrim ( substring ( @s , patindex ( ' %.exe% ' , @s ) + 4 , len ( @s )))
select @s = stuff ( @s , 1 , patindex ( ' %[^0-9]% ' , @s ) - 1 , '' )
,
@s = stuff ( @s , 1 , patindex ( ' %[0-9]% ' , @s ) - 1 , '' )
,
@s = rtrim ( left ( @s , patindex ( ' % % ' , @s ) - 1 ))
end
else if @i = 5 -- memory
begin
if patindex ( ' %.exe% ' , @s ) = 0
set @s = stuff ( @s , 1 , patindex ( ' %[0-9]% ' , @s ) - 1 , '' )
else
set @s = ltrim ( substring ( @s , patindex ( ' %.exe% ' , @s ) + 4 , len ( @s )))

select @s = stuff ( @s , 1 , patindex ( ' %[0-9]% ' , @s ) - 1 , '' )
,
@s = stuff ( @s , 1 , patindex ( ' %[^0-9]% ' , @s ) - 1 , '' )
,
@s = stuff ( @s , 1 , patindex ( ' %[0-9]% ' , @s ) - 1 , '' )
,
@s = stuff ( @s , 1 , patindex ( ' %[1-9]% ' , @s ) - 1 , '' )
,
@s = rtrim ( left ( @s , patindex ( ' %k% ' , @s ) - 1 ))
end
else if @i = 6 -- user_name
select @s = stuff ( @s , 1 , patindex ( ' %[0-9]% ' , @s ) - 1 , '' )
,
@s = stuff ( @s , 1 , patindex ( ' %[^0-9]% ' , @s ) - 1 , '' )
,
@s = stuff ( @s , 1 , patindex ( ' %[0-9]% ' , @s ) - 1 , '' )
,
@s = stuff ( @s , 1 , patindex ( ' %[1-9]% ' , @s ) - 1 , '' )
,
@s = rtrim ( substring ( @s , patindex ( ' %\% ' , @s ) + 1 , 18 ))
else if @i = 7 -- cup_time
select
@s = substring ( @s , patindex ( ' %[0-9]:[0-9][0-9]:[0-9][0-9]% ' , @s ), 8 )
else
select @s =right ( @s , charindex ( ' ' , reverse ( @s ), 3 ) - 1 )
return @s
end

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
复制代码
复制代码



相关文章
|
17小时前
|
SQL 关系型数据库 MySQL
【Go语言专栏】使用Go语言连接MySQL数据库
【4月更文挑战第30天】本文介绍了如何使用Go语言连接和操作MySQL数据库,包括选择`go-sql-driver/mysql`驱动、安装导入、建立连接、执行SQL查询、插入/更新/删除操作、事务处理以及性能优化和最佳实践。通过示例代码,展示了连接数据库、使用连接池、事务管理和性能调优的方法,帮助开发者构建高效、稳定的Web应用。
|
17小时前
|
关系型数据库 MySQL 数据库
mysql 设置环境变量与未设置环境变量连接数据库的区别
设置与未设置MySQL环境变量在连接数据库时主要区别在于命令输入方式和系统便捷性。设置环境变量后,可直接使用`mysql -u 用户名 -p`命令连接,而无需指定完整路径,提升便利性和灵活性。未设置时,需输入完整路径如`C:\Program Files\MySQL\...`,操作繁琐且易错。为提高效率和减少错误,推荐安装后设置环境变量。[查看视频讲解](https://www.bilibili.com/video/BV1vH4y137HC/)。
22 3
mysql 设置环境变量与未设置环境变量连接数据库的区别
|
17小时前
|
Oracle Java 关系型数据库
【服务器】python通过JDBC连接到位于Linux远程服务器上的Oracle数据库
【服务器】python通过JDBC连接到位于Linux远程服务器上的Oracle数据库
14 6
|
17小时前
|
SQL Java 关系型数据库
【JAVA基础篇教学】第十六篇:Java连接和操作MySQL数据库
【JAVA基础篇教学】第十六篇:Java连接和操作MySQL数据库
|
17小时前
|
SQL Java 数据库连接
Java数据库编程实践:连接与操作数据库
Java数据库编程实践:连接与操作数据库
11 0
|
17小时前
|
关系型数据库 Java 数据库
docker部署postgresql数据库和整合springboot连接数据源
docker部署postgresql数据库和整合springboot连接数据源
17 0
|
17小时前
|
SQL JSON 关系型数据库
[UE虚幻引擎插件DTPostgreSQL] PostgreSQL Connector 使用蓝图连接操作 PostgreSQL 数据库说明
本插件主要是支持在UE蓝图中连接和操作PostgreSQL 数据库。
18 2
|
17小时前
|
Java 关系型数据库 数据库连接
【C 言专栏】C 语言与数据库的连接与操作
【5月更文挑战第2天】本文探讨了C语言如何连接和操作数据库,介绍了数据库连接的基本原理,如通过ODBC、JDBC或原生接口与数据库交互。文章详细阐述了使用ODBC连接的步骤,并列举了C语言在数据库操作中的常见任务,强调了错误处理、数据类型匹配和性能优化的重要性。通过实际案例,展示了在学生信息管理系统中应用C语言与数据库交互的过程。本文旨在帮助读者更好地理解和应用C语言进行数据库管理。
|
17小时前
|
关系型数据库 MySQL PHP
【PHP 开发专栏】PHP 连接 MySQL 数据库的方法
【4月更文挑战第30天】本文介绍了 PHP 连接 MySQL 的两种主要方法:mysqli 和 PDO 扩展,包括连接、查询和处理结果的基本步骤。还讨论了连接参数设置、常见问题及解决方法,如连接失败、权限和字符集问题。此外,提到了高级技巧如使用连接池和缓存连接信息以优化性能。最后,通过实际案例分析了在用户登录系统和数据管理中的应用。
|
17小时前
|
运维 分布式计算 DataWorks
DataWorks产品使用合集之dataworks为子账号创建DataWorks访问密钥的基本步骤如何解决
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
28 0