开发者

Why Session objects are not removed after Timeout period in Asp.Net?

开发者 https://www.devze.com 2023-02-09 13:01 出处:网络
Why Session objects are not removed after Timeout period? I am using Asp.Net 4.0 and Session state is configured as shown below.

Why Session objects are not removed after Timeout period?

I am using Asp.Net 4.0 and Session state is configured as shown below.

<s开发者_开发百科essionState mode="SQLServer" cookieless="false" timeout="5"
                  allowCustomSqlDatabase="true" 
                  sqlConnectionString="data source=.\SqlExpress;initial catalog=App_SessionState;user id=sa;password=xxxxxxxx"/>

If I have not activity in browser for about 10 mins, shouldn't the Session object be removed. But after 10 mins I can still access the Session variable. Am I missing something here?

EDIT:

If I access a session variable after 10 mins as shown below shouldn't I get NULL

  var myObj = Session["MyKey"] as MyClass;

mObj is not NULL after 10 mins.


There's a stored procedure installed called DeleteExpiredSessions, called from the job ASPState_Job_DeleteExpiredSessions, and is executed every minute (if I read the InstallSqlState.sql file correctly).

The procedure basically calls DELETE FROM ASPStateTempSessions WHERE Expires < GETUTCDATE()

So, if objects aren't removed, check the Expires column, and verify that you're comparing with the utc date. If in doubt, do a SELECT * FROM ASPStateTempSessions WHERE Expires < GETUTCDATE(). Also, make sure that your ASPState_Job_DeleteExpiresSessions is enabled and working.

A quick (and totally unconfirmed idea); do SQL Server Express come with the SQL Agent? Is it enabled and able execute scheduled jobs?


The "session" is never "null", but after the timeout has expired, the session object is emptied (or re-instantiated), another session is automatically started (you can check this by handling SessionEnd and SessionStart events), and you will always have a reference to a session object. Does not happen? Still you see previous session's data?


To add onto what Simon said, If there is no SQL Server Agent running there will be no clearing of session values unless a stored procedure inside of the database is actually executed.
I don't think SQL Server express has the Agent so an automated job is not possible.

If you have control of the server then I would suggest setting up a scheduled task through windows that executes the stored procedure or job that clears your expired sessions. I don't know the exact name of the stored procedure right now but it should be named fairly obvious to it's purpose.

So your options are to upgrade to a version of SQL server that has the SQL Server Agent available or set something up to manually execute the stored procedure to clear expired sessions.

Alliteratively you can use InProc sessions, which are cleared automatically. But I assumed since InProc is the default there is a reason why you switched to SQL Server.

0

精彩评论

暂无评论...
验证码 换一张
取 消