Read more: http://www.blogsmonetize.com/2010/10/how-to-use-syntax-highlighter-3083-in.html#ixzz1DHzvEgBA

Tuesday, May 3, 2011

How to restrict number of connections to a database

Today, one of my friend at workstation asked me "How to restrict the number of users connected to a DB in a SQL server"?

Initially, my answer was as easy as to configure the user connection option to the required number using sp_configure.


sp_configure 'user connections',100
RECONFIGURE


There is a break!!!It can only be done at server level. What about at DB level???

From SQL Server 2005 sp2, there a feature called LOGON Trigger. How about using the feature???Yes, it is very well possible
using Logon trigger by rolling back the connections once it reached a maximum required connections.It is not at DB level, but through Login.



create trigger AuditLogin
on database_Name
with execute as self
for logon
as
begin
IF ORIGINAL_LOGIN()= '< Login Name > ' AND
(SELECT COUNT(*) FROM sys.dm_exec_sessions
WHERE is_user_process = 1 AND
original_login_name = '< Login Name > ) > 10
ROLLBACK;
end

No comments: