清理数据库连接线程

简介:
复制代码

 
 
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
复制代码
复制代码
相关文章
|
关系型数据库 MySQL Java
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
|
数据库连接 应用服务中间件 PHP
|
安全 Linux 网络安全
YashanDB数据库服务端SSL连接配置
YashanDB支持通过SSL连接确保数据传输安全,需在服务端生成根证书、服务器证书及DH文件,并将根证书提供给客户端以完成身份验证。服务端配置包括使用OpenSSL工具生成证书、设置SSL参数并重启数据库;客户端则需下载根证书并正确配置环境变量与`yasc_env.ini`文件。注意:启用SSL后,所有客户端必须持有根证书才能连接,且SSL与密码认证独立运行。
|
SQL Java 关系型数据库
Java连接MySQL数据库环境设置指南
请注意,在实际部署时应该避免将敏感信息(如用户名和密码)硬编码在源码文件里面;应该使用配置文件或者环境变量等更为安全可靠地方式管理这些信息。此外,在处理大量数据时考虑使用PreparedStatement而不是Statement可以提高性能并防止SQL注入攻击;同时也要注意正确处理异常情况,并且确保所有打开过得资源都被正确关闭释放掉以防止内存泄漏等问题发生。
611 13
|
SQL 关系型数据库 MySQL
MySQL数据库连接过多(Too many connections)错误处理策略
综上所述,“Too many connections”错误处理策略涉及从具体参数配置到代码层面再到系统与架构设计全方位考量与改进。每项措施都需根据具体环境进行定制化调整,并且在执行任何变更前建议先行测试评估可能带来影响。
2092 11
|
关系型数据库 MySQL 数据库连接
数据库连接工具连接mysql提示:“Host ‘172.23.0.1‘ is not allowed to connect to this MySQL server“
docker-compose部署mysql8服务后,连接时提示不允许连接问题解决
710 69
|
Oracle 安全 关系型数据库
【Oracle】使用Navicat Premium连接Oracle数据库两种方法
以上就是两种使用Navicat Premium连接Oracle数据库的方法介绍,希望对你有所帮助!
3047 28
|
SQL 缓存 分布式数据库
【赵渝强老师】达梦数据库的线程结构
达梦数据库采用单进程、多线程结构,利用对称服务器架构实现高效资源利用与可扩展性。其核心线程类型包括监听线程(管理客户端连接)、I/O线程(处理数据页读写)、工作线程(执行数据操作)、调度线程(定时任务管理)和日志刷新线程(确保REDO日志刷盘)。通过合理分工与同步机制,达梦数据库实现了高性能与稳定性。视频讲解进一步详细介绍了各线程的功能与协作方式。
581 1
【赵渝强老师】达梦数据库的线程结构

热门文章

最新文章