如何自动关闭0(零)CPU使用率SQL连接? [英] How to automatically close 0(zero) CPU usage SQL connections?

查看:194
本文介绍了如何自动关闭0(零)CPU使用率SQL连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Web应用程序和Android APP WEB API使用的是同一数据库,问题是0(零)CPU使用率连接没有自动关闭,我必须手动运行代码才能杀死它们.

我尝试过的事情:

我必须手动执行此操作,而我需要自动关闭它

My Web Application and Android APP WEB API are using same database,the problem is 0(Zero) CPU usage connections are not closing automatically i have to run the code manually to kill them.

What I have tried:

I have to execute this manually instead i need to close it automatically

set nocount on
declare @databasename varchar(100)
declare @query varchar(max)
set @query = ''

set @databasename = 'xxx'
if db_id(@databasename) < 4
begin
	print 'system database connection cannot be killeed'
return
end

select @query=coalesce(@query,',' )+'kill '+convert(varchar, spid)+ '; '
from master..sysprocesses where dbid=db_id(@databasename)

if len(@query) > 0
begin
print @query
	exec(@query)
end

推荐答案

为什么要强制从服务器关闭打开的连接边?

处理连接的正确方法是从客户端打开和关闭它们.从服务器端关闭连接应该是一个罕见的例外.

该数据库旨在同时处理多个连接.其中一些活动,一些闲置.基本上,空闲连接不会造成太大的伤害,但是正如所说的那样,一个好的软件设计不会使连接保持不必要的打开状态.
Why do you want to close an open connection by force from the server side?

The correct way to handle connections is to open and close them from client side. Closing a connection from the server side should be a rare exception.

The database is designed to handle multiple connections at the same time. Some of them active, some idle. Basically an idle connection does not hurt too much but as said a good software design does not keep connections open unnecessarily.


这篇关于如何自动关闭0(零)CPU使用率SQL连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆