Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lifetime and scope of session variables?

Status
Not open for further replies.

Nelviticus

Programmer
Sep 9, 2003
1,819
GB
I have an application that lets users edit various details of our clients. When the app starts it reads a list of clients from an SQL database and stores this in a global session variable so that it can be displayed quickly when needed.

However, if another user adds a new client to the database the person using my app won't see it in the client list for a while, because when they view the client list they aren't viewing the 'live' one but the stored one.

If a third user starts my app just after the new client has been added they also cannot see the new client, because they are viewing the list that the first user stored in the session.

So, how long do session variables last for and who do they apply to? As you can guess I'm quite new to this. I'm using C# code behind the pages and I use 'Session.Add(VariableName, Value)' to store the list. It seemed like a good idea at the time!

Nelviticus
 
Session variables only apply to the current user of the app, and thus are not suited to the kind of thing you are trying to do here. You are correct to use global variables. Session variables last until session.abandon is called or the session times out, as set in the web.config file.

The pattern I've generally used is to load stuff into a global object, as you are doing, and when the user adds something, add it to the global object. You could either write to the db at the same time, or wait until the app shuts down (the Application_end event in the global.asax file) to write the information to the db.

Finally, look up the methods Application.Lock and Application.Unlock if you choose to use this method.

Hope this helps

Mark [openup]
 
Hmm, well I've just started my app myself and I can't see the new client, yet it's there when I look at the database with Query Analyser. This seems to indicate that I am viewing the list which the first user stored in the session, which would mean that his session variables are also applying to me.

A separate third-party application is used for creating new clients so I can't refresh my list when one is added because I can't monitor that event.

Nelviticus
 
OK - you did not specify that it was another app adding the clients, but no worries.

In this case, you have some different choices. You might want to look at caching with a dependency on the db table - this means that your app can read the list and store it in memory, and then every time your code asks for the list, the app will automatically check the db to see if the info has changed, based on a timestamp. I don't use SQL server, so I don't know much more than that. You can simulate the same thing by placing a trigger on the table and in the trigger changing a file's timestamp. You then place the cache dependency on the file rather than the table.

This would be the most efficient pattern in this case.

Alternatively, you might just choose to read from the db every time you need the list - or just once during page load, and store the results in a page level variable.

Finally, just to clarify, there are two terms which get confused.

Application - this state is accessed via the Application object. It is what I meant in my previous post.
Global Variables - I think these are specific to VB and are not what I meant in the previous post, sorry.

The symptoms as you describe them mean that unless there is something else I am missing, you must be using Application state, and not Session state.


Mark [openup]
 
Sorry - that last sentence was rubbish

"The symptoms as you describe them mean that unless there is something else I am missing, you must be using Application state, and not Session state"

Ignore it.

Mark [openup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top