sql server 2000: how to limit a users session connecting to the database.

  • if a user connects from the application end to the database, he gets only 30 minutes of session time after which the session gets expired and he has to connect again.

    how do i implement it in SQL SERVER 2000.

  • Not easy to do in SQL 2000.

    However, let me ask, why would you ever want to do this?

    -- Gianluca Sartori

  • a request from the client side, the problem is there are no logon triggers in sql server 2000, so is it feasible to write a procedure to check the session time for that particular user and schedule it to run every 5 min?

    or is there any other way to do it?

  • That's how I would do it.

    Even if SQL Server 2000 had logon triggers, it would not help, as you would have to check for existing sessions, not for new ones.

    -- Gianluca Sartori

  • having a small doubt(silly one)

    if i write a logon trigger(forget the sql version), is it like : after the completion of trigger the user gets the access or he gets the access while the trigger is running?

  • Not sure what you mean.

    The logon trigger is executed as part of the log on process. The session is established before the trigger runs, but the session is not available to the user until the trigger code completes.

    I hope this clarifies things instead of complicating...

    -- Gianluca Sartori

  • thanks for resolving my doubt!

    Is there any other way instead of procedures to accomplish session time limiting in sql server 2000?

    if it is sql server 2008, what's the way?

  • Not very different in SQL 2008.

    Is there any chance to let the application do that?

    -- Gianluca Sartori

  • yes that can be done,

    i am working on the issue from my end by using (sys.dm_exec_sessions) or should i use something else?

  • In SQL2008 you probably want to use sys.dm_exec_sessions and identify sessions that have been idle for the last 30 minutes:

    DECLARE @idle_timeout int = 30; -- minutes

    SELECT session_id

    ,status

    ,login_time

    ,host_name

    ,program_name

    ,host_process_id

    ,original_login_name

    FROM sys.dm_exec_sessions

    WHERE status = 'sleeping'

    AND last_request_end_time < DATEADD(minute, -1 * @idle_timeout, GETDATE())

    AND is_user_process = 1

    -- Gianluca Sartori

  • i am sure that will work, but sys.dm_exec_sessions is not their in sql server 2000 !

  • thanks for the help!

  • sql-noob (8/8/2012)


    i am sure that will work, but sys.dm_exec_sessions is not their in sql server 2000 !

    In SQL 2000 you could use sysprocesses, which returns almost the same information.

    -- Gianluca Sartori

  • i wrote the following procedure

    if theres any correction please notify, i would be happy for your input on this

    -------------------------------------------------------------------------------

    /* creating a procedure*/

    create/replace procedure <procedure_name>

    as

    decalre @sessid smallint

    declare @idle_time_out = 30 -- minutes

    declare @count int

    declare @counter = 1

    begin

    /* creating a new table dbo.testkill having the values for the spid for more then 30 minutes*/

    create table dbo.testkill

    (

    id int identity(1,1),

    spid smallint

    );

    select spid into dbo.testkill from dbo.sysprocesses where spid>50 AND loginame='<login_name>'

    AND login_time < dateadd(minute,-1*@idle_time_out,getdate());

    select @count=count(*) from dbo.testkill;

    set @count=@count+1

    /* setting the while loop*/

    while @counter<@count

    begin

    select @sessid=spid from dbo.testkill where id=@counter;

    kill @sessid;

    set @counter=@counter+1

    end;

    /* end of while loop */

    drop table dbo.testkill;

    end;

  • Let me suggest a couple of minor changes:

    CREATE/ALTER PROCEDURE < PROCEDURE_NAME >

    AS

    BEGIN

    DECLARE @sessid SMALLINT

    DECLARE @idle_time_out INT;

    DECLARE @sql varchar(20);

    SET @idle_time_out = 30 -- minutes

    /* creating a new table dbo.testkill having the values for the spid for more then 30 minutes*/

    DECLARE @testkill TABLE (

    id INT identity(1, 1)

    ,spid SMALLINT

    );

    DECLARE sessions CURSOR STATIC LOCAL FORWARD_ONLY

    FOR

    SELECT spid

    INTO dbo.testkill

    FROM dbo.sysprocesses

    WHERE spid > 50

    AND loginame = '<login_name>'

    AND login_time < dateadd(minute, - 1 * @idle_time_out, getdate());

    OPEN sessions

    FETCH NEXT FROM sessions INTO @sessid

    /* setting the while loop*/

    WHILE @@FETCH_STATUS = 0

    BEGIN

    SET @sql = 'KILL ' + CAST(@sessid AS varchar(10));

    EXEC(@sql);

    FETCH NEXT FROM sessions INTO @sessid

    END;

    /* end of while loop */

    CLOSE sessions

    DEALLOCATE sessions

    END

    -- Gianluca Sartori

Viewing 15 posts - 1 through 15 (of 16 total)

You must be logged in to reply to this topic. Login to reply