Solved

Global variables in .Net with multi-users are getting switched.

Posted on 2012-03-19
11
578 Views
Last Modified: 2012-06-21
We have a .Net web application. Due to many reasons, I created a Public Class in App_Code folder. It has, among others:
Private Shared _AltPopNum As Integer

  Public Shared Property AltPopNum() As Integer
    Get
      Return _AltPopNum
    End Get
    Set(value As Integer)
      _AltPopNum = value
    End Set
  End Property

Open in new window

Apparently, everything worked fine. A couple pages set this value, which can then be accessed in another couple pages. But recently, a case occured where this value was switched between users. That is, UserA inserted the value 10. Two seconds later, UserB inserted the value 15. When UserA read the variable, it was assigned as 15.

Now, I was under the impression that global class variables were stored pretty much like session variables, that is, they are linked to the username/computer. A user in one computer would have totally separate variables from those of another user in another computer. I decided to use this approach because these are short term variables (usually used within the next minute or two and then become useless) and I already had the class created for other methods. But if this isn't reliable, then I'd have to switch to keeping these values in a table and connecting to the database all the time, which I'd rather not do. Session variables would share the same problems, despite being shorter lived (we've tried to make them stay alive for more than 20 minutes, but utterly failed, despite trying various different solutions).

So, does anyone know why this is happening? Is this to be expected from using global variables? Or is this just some freak problem? Can it be solved via some settings, on .Net or IIS? Will I have to move these variables to a table in the DB? What should be the best approach?

EDIT: It should also be noted that this particular user kept having wrong values returned for some time (to my knowledge no other changes occured within seconds of it), then changed computer and, apparently, things worked fine.
0
Comment
Question by:Cluskitt
  • 6
  • 5
11 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37738909
Because of the very nature of shared variables, they will be created one in an AppDomain and will remain till the AppDomain Unloads, so its always advisable to synchronize the access to shared variables if they are intended to by used by many threads at one point in time.
This can be done using Monitor.Enter and Monitor.Exit Methods or SyncLock VB Statement

Here is a tutorial to help you on that:-
http://www.developerfusion.com/article/5184/multithreading-in-vbnet/2/
0
 
LVL 18

Author Comment

by:Cluskitt
ID: 37738984
Does that apply to a web application? There aren't multiple threads, just multiple sessions on different computers. Maybe I'm just being a bit thick (I'm fairly decent at desktop programming, but I hate web dev), but it seems to me that it applies only to a desktop app?
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37739019
yes it does apply to web, as each request i handled in separate thread....
Just synchronize the shared variable and see the effect....

Read here:-
http://technicalsol.blogspot.in/2009/04/aspnet-static-variable.html

Summary : Either get rid of them(Preferred) or synchronize them
0
 
LVL 18

Author Comment

by:Cluskitt
ID: 37741191
Ok. This, however, seems to apply for multiple instances on the same computer. Which makes sense, because it would share the same session/cookies/etc. But my problem happened with two different computers, on two different locations. Shouldn't those be handled separately? Or does the ASP.Net engine simply consider any connection as a thread?

I could fix this easily with url redirection, but I don't wish to have any data on the URL where it can be manipulated.

Also, it seems to me that the purpose of the first link you provided is to prevent access to variables while they're being written. And that isn't the problem I'm having. The problem I have is that, using my above example, UserA should have a value of 10, even though UserB has a value of 15 for the same variable. In fact, UserA should have a value of 10 even if 50 different users assign a value to their variable. Each variable should be user independent.
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37741558
To Make Each Variable User Independent and maintain state, it has to be an instance variable and you have to manage state by saving the instance of the type in cache or session.

class MyClass
{
private int myValue;

public MyClass(int value)
{
myValue=value;
}
}

MyClass obj=new MyClass(10);
Session["MyClass"]=obj;

Open in new window



If you keep on using Shared Variables, they will be shared in many instances of same type in different threads as each request is handled separately by IIS.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 18

Author Comment

by:Cluskitt
ID: 37741584
So, the only solution for keeping variables User Independent without using session (or url redirection), would be DB based, right? Luckily, we don't use those for that many pages, so I won't have to replace much code, but we do use those pages quite a bit. I was hoping for a way to store them locally without session variables, due to the difficulties in maintaining their persistence.
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37741588
another way is to store them on client in some hidden text boxes...
If your data is relatively lesser, then try for this approach...
0
 
LVL 18

Author Comment

by:Cluskitt
ID: 37741596
It is lesser, but it has to carry across pages, which is why I used the global variables route. Basically, half a dozen variables are set in one page, which then have to be accessed by another different page, which uses those variables for some parametrization.
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37741607
use cookies then....
0
 
LVL 18

Author Comment

by:Cluskitt
ID: 37741625
Those are pretty much the same as session variables and can be tampered with like url redirection. I guess I will have to use DB based variables. It will be a small table anyway. Even if we have about 100 users, we won't have more than 2000 records at any time. With the added bonus that variables will carry over different sessions, which might actually have its uses. Thanks for your help. I'm going to close the question now.
0
 
LVL 18

Author Closing Comment

by:Cluskitt
ID: 37741629
Thanks for the help.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Read about why website design really matters in today's demanding market.
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now