[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1592
  • Last Modified:

using session variables vs. storing them in a database table, c# asp.net

I had a bit problem with my session variables; thus switched to use a sql server table to store my values between the sessions. I intend to go back and use session variables again, however, I like to learn a bit more about advantages/disadvantages of these two methods.

Question: What are the advantages of session variables over storing the values in a SQL Server table between the sessions? And visa verse.

Could you possibly list the key points for using session variables?

Thank you
0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 3
  • 2
2 Solutions
 
käµfm³d 👽Commented:
By "session variables" I assume you are referring to in-memory storage (on the web server), because you can also have session stored in the database as well. (By that I am not referring to what I think you are doing, but rather you still do Session["blah"], but it's stored in the database.) In-memory sessions will be orders of magnitude faster than a database because you are dealing with local memory on the web server. With a database you eat the cost traversing the network to get to the database itself. With databases you also have to set up your table structure, whereas with in-memory you just stick a POCO into the session. In-memory, because it is in memory, will consume resources on the web server. If you are storing large things in session, then you don't want to use in-memory because every user that is logged in might have one or more of these large objects in their own session, consuming memory. With a database, this is not as big of a concern, but again, you are sending these large objects back and forth across the network.
0
 
Dave BaldwinFixer of ProblemsCommented:
Session variables are short term storage while databases entries are 'permanent'.  Session data expires after I think 30 minutes of inactivity while the database entries don't expire at all.
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
kaufmed,

- The values stored are short strings like "2014", "Site 1", etc.

Could you please comment on the following statements?

- Session variable: expires in set number of minutes or when a session ends.
- Example of session available is Session["YYYY"]="2014";

- Application variable expires only when all users quit the application.
- Please give a sample (syntax) of how an application variable is set:

- Application variable shouldn't be used for something like ="2014"; because different users may be working with different years values and that will cause conflict.

-use application variables for commonly shared data pieces all visitors may use.

Sorry for the long list. Respond to as many of them you like.

Regards,

Mike

Edited:
Dave Baldwin,
Thank you for the post. I didn't see your comment prior to my post.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
käµfm³d 👽Commented:
Application variable expires only when all users quit the application.
Not accurate. Application variables will hold their values as long as the application is loaded into memory. How long this is is configured in your IIS setup (though I believe the clock does start running once there has been no activity on the site in X number of minutes).

Please give a sample (syntax) of how an application variable is set:
Use the Application property: http://msdn.microsoft.com/en-us/library/vstudio/94xkskdf(v=vs.100).aspx

Application variable shouldn't be used for something like ="2014"; because different users may be working
Perhaps. If all users need to see the same value, and no one will modify it, then an application variable might make sense. If users can update the data, then you'll need to think it through.

use application variables for commonly shared data pieces all visitors may use
Agreed. An application variable is really more for storing some aspect of the application itself. This may or may not have a visible effect to your users.
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you.
0
 
käµfm³d 👽Commented:
Correction to Dave Baldwin's comment:

Session timeout is configurable per application, and the default value is 20 minutes.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now