Timer in T - SQL

  • Hi All,

    Greetings for the day!!

    Have come across one case not able to find out how to go ahead with this.

    I am integrating SQL Server with website(asp.net) for some project. Now what i am trying to do is when user is logging into the website we are capturing login details (Login ID, login Time etc) in SQL Table.

    So case wherein User is not doing anything on the website then i want 'Login expired'. For that i need to calculate at SQL table ( Make Login Active flag = FALSE).

    But since there is no event fired from website (Session is ideal and user in not doing anythig) i am not able to figure out how to give session expired.

    Is there something in SQL called timer which can automatically record the login time and make the Active flag false when the time expires.

    Thanks a lot in advance.

    Regards.

  • Quick thought, use a calculated column for an "active" flag, here is an example, not a full solution but should be enough to get you passed this hurdle

    😎

    USE tempdb;

    GO

    SET NOCOUNT ON;

    IF OBJECT_ID('dbo.TBL_SESSION_LOG') IS NOT NULL DROP TABLE dbo.TBL_SESSION_LOG;

    CREATE TABLE dbo.TBL_SESSION_LOG

    (

    SESSION_LOG_ID INT IDENTITY(1,1) NOT NULL CONSTRAINT PK_DBO_TBL_SESSION_LOG_SESSION_LOG_ID PRIMARY KEY CLUSTERED

    ,SESSION_ACTIVE AS (1 - SIGN(1 - SIGN(20 - DATEDIFF(SECOND,SESSION_TIMESTAMP,GETDATE()))))

    ,SESSION_TIMESTAMP DATETIME NOT NULL CONSTRAINT DFLT_DBO_TBL_SESSION_LOG_SESSION_TIMESTAMP DEFAULT (GETDATE())

    ,USERNAME VARCHAR(50) NOT NULL

    );

    GO

    INSERT INTO dbo.TBL_SESSION_LOG (USERNAME) VALUES ('USER_ABC');

    GO

    SELECT * FROM dbo.TBL_SESSION_LOG;

    GO

    WAITFOR DELAY '00:00:10';

    GO

    SELECT * FROM dbo.TBL_SESSION_LOG;

    GO

    INSERT INTO dbo.TBL_SESSION_LOG (USERNAME) VALUES ('USER_DEF');

    GO

    SELECT * FROM dbo.TBL_SESSION_LOG;

    GO

    WAITFOR DELAY '00:00:10';

    GO

    SELECT * FROM dbo.TBL_SESSION_LOG;

    GO

    GO

    WAITFOR DELAY '00:00:10';

    SELECT * FROM dbo.TBL_SESSION_LOG;

    GO

  • seems to me that an idle timeout could be handled better via the web app directly (example). Keeping the session state in the database would require your app to constantly check that state, thus increasing traffic and resource usage on your database.

    Bob
    -----------------------------------------------------------------------------
    How to post to get the best help[/url]

  • Robert klimes (12/3/2014)


    seems to me that an idle timeout could be handled better via the web app directly (example). Keeping the session state in the database would require your app to constantly check that state, thus increasing traffic and resource usage on your database.

    +1

    Session timeouts due to inactivity should be handled in the front end.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Agreed this should be handled at the web app level not the db level, not only would the web app constantly have to be checking the db for the active state it would also have to update the db everytime the user did anything.

    Website time outs are a pretty standard feature and I'm pretty sure most of them don't really on tracking it in the DB.

  • Thanks a lot Folks!!

    I will try to update at web app side.

    Thanks once again

Viewing 6 posts - 1 through 5 (of 5 total)

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