Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static variables in web applications

Can I use static variables in my web application ? what are the alternatives to static ? When I use static variables in pages and more than one user use the application, it makes conflict data (incorrect data).

What are the limits of using static members?

Are static members shared in memory?

like image 645
Anyname Donotcare Avatar asked Mar 07 '11 11:03

Anyname Donotcare


People also ask

What is static variable with example?

1) A static int variable remains in memory while the program is running. A normal or auto variable is destroyed when a function call where the variable was declared is over. For example, we can use static int to count a number of times a function is called, but an auto variable can't be used for this purpose.

What are static variables used for?

Static variables are used to keep track of information that relates logically to an entire class, as opposed to information that varies from instance to instance.

Is there a static variable in JavaScript?

Static variable in JavaScript: We used the static keyword to make a variable static just like the constant variable is defined using the const keyword. It is set at the run time and such type of variable works as a global variable. We can use the static variable anywhere.

What is static variable in visual programming?

A static variable continues to exist and retains its most recent value. The next time your code calls the procedure, the variable is not reinitialized, and it still holds the latest value that you assigned to it. A static variable continues to exist for the lifetime of the class or module that it is defined in.


2 Answers

Consider storing your shared variables in the HttpApplication object or in the Cache object.

However, if you are trying to store values for each user separately, you should store those values in a Session variable.

Static variables inside Asp.Net are shared in the memory space of the w3svc.exe process and are NOT thread-safe. They can be accessed and modified by any user of the application. This could lead to unwanted modifications, unless you write your own lock mechanism around the storage of those values.

You should try a syntax like:

Application["KEY"] = value;

to store shared application-wide data and

Session["KEY"] = value;

to store data on a per-user basis

You can use the WebCache object to store data in the web server's memory with expiration conditions. Syntax for that looks similar to:

Page.Cache.Insert("KEY", "VALUE", null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);

More on the syntax of managing the WebCache object can be found at:

http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx

like image 173
Jeff Fritz Avatar answered Oct 08 '22 09:10

Jeff Fritz


Just to add to what @Jeff Fritz has said, an IIS application creates an AppDomain in which your assemblies are loaded. Just like the rules of a normal windows application, if a class such as

public static class Something
{
    public static string SomeString { get; set; }
}

...then only a single Something.SomeString property is available per AppDomain. The W3SVC process manages the AppDomain, but the is no guaruntee of thread safety (as an AppDomain can be service multiple requests). If you are using read-only static properties, it's probably fine (such as reading config values). If you are making mutable properties that change through the lifetime of a request, its better to use one of the storage mechanisms detailed in other questions here.

like image 44
Matthew Abbott Avatar answered Oct 08 '22 07:10

Matthew Abbott