我被要求将SQL Server配置为每次登录只允许一个会话。我已经找到了一些关于创建登录触发器的引用,以防止登录建立多个会话,但我想知道是否有某种方法可以在较低级别定义它,所以这个会话限制是默认的,而不是而不是必须在每个用户的另一个登录中定义它?
我在stackoverflow上的“可能已经有你的答案的问题”和“类似的问题”中看到了很多关于这个主题的引用,但到目前为止还没有找到或者没有理解描述我是什么的帖子试图做。我还看到了一个关于声明式管理框架的参考资料,它允许您按照我认为的策略配置SQL Server。
我将继续在这里查看文章以尝试学习这一点,但在此期间...建议非常感谢!
答案 0 :(得分:3)
联机丛书中logon trigger的示例与我认为您想要的非常接近,我做了一些更改,使其适用于所有登录。
-- Trigger must be created by a user with 'view server state' permission in order the trigger to have unrestricted access to sys.dm_exec_sessions.
create trigger connection_limit_trigger on all server with execute as self for logon
as
begin
-- Check whether the caller is a SysAdmin.
-- Note: The trigger is executing under an elevated security context so we must switch to the caller's context to test their SysAdmin status.
declare @IsSysAdmin int
execute as caller
set @IsSysAdmin = isnull(is_srvrolemember ('sysadmin'), 0)
revert
-- If the user is not a SysAdmin and there are already too many non-dormant sessions for this login then 'rollback' the logon.
-- Note: sys.dm_exec_sessions does not include an entry for this logon attempt.
if ((@IsSysAdmin = 0) and ((select count(1) from sys.dm_exec_sessions where is_user_process = 1 and status <> 'Dormant' and original_login_name = original_login()) > 1))
begin
raiserror('This login has exceeded the maximum of 1 connections to this SQL Server.', 16, 1)
rollback
end
end
我已添加支票,因此该限制不适用于SysAdmin登录,也不会计算休眠连接池连接数。有几点需要注意;