views:

124

answers:

5

0 I have an ASP.NET 3.0 application that works fine on one server. It uses application variables to check if a user has checked out a file and locks it for all other users. When I put the application on the load balanced servers, the application didnt work as expected since it multiple users got sent to multiple servers and each user could check out the requred file.

The main point is that, is there any way I can share the application variables in my application even though it is distributed on multiple servers. Or is there a better way to get a global variable?

EDIT: I am checking if a file is checked out by Application["FileLocked"] is true or false

A: 

take a look at this

Fredou
+2  A: 

How are you telling the system a file is checked out? I hope you are not using session variables. You should store the state in a database field and then check if the field (bool / bit) is checked / not checked. Whether the system is on multiple servers or one doesn't matter. The connection to the application will use the same connection string, hence the same data.

JonH
is am using the Application state variable. Application["FileLocked"]
TP
Ouch...See if you can modify and store this in a database table.
JonH
Yep, use the database, store the state (checked out) and who has it (context), as a plus, you can manage the state and context if the user forgets to check it back in.Send them alerts, manual method to unlock it, lock the account they use, whatever, you get to decide how to manage the use and the proper reponse to the check-in exception event.
Mark Schultheiss
+2  A: 

Two things for you to consider:

  1. In a load balanced environment, instance data within one server will not be available to another. While you can use Out-of-process session state, you can't easily make Cache items or static class data available across servers. A user's request will not always be routed to the same server (unless you use something like sticky sessions) - which can cause other problems.

  2. You probably should use a more persistent mechanism (like a lock file) to determine whether a particular user is using a file. Otherwise, you may introduce race conditions in your system - and if a user's sessions ends without them unlocking the file you'll end up with orphaned locked files in your system.

LBushkin
A: 

I think this is a task for a database. If you don't have one already, create a MySQL or other RDBMS instance somewhere and use that to share information like that. If you use the application memory to store that information, it will get lost whenever the app pool has to be recycled or the webserver is restarted.

DrJokepu
+2  A: 

Application variables are stored within ASP.NET, which means that you'll have duplicate variables between servers.

As JonH suggested, store this in the database. That's where this type of information is supposed to go. Reasons:

  1. Eliminates load-balancing effects on session and application variables
  2. Maintains values even if the application resets. Normally, an ASP.NET app is unloaded (?) after 20 minutes of inactivity. With your scheme, even on a single server, this means that if someone checks out a file, goes home, and comes back the next day, it will no longer be checked out. Storing this information in a database eliminates that problem.
David Lively