Building WebApps  «Prev  

Timing Problem with a Shared Application Variable

Our website is using an application variable to track the current number of users accessing the site at any time. This variable is increased or decreased in value as part of the Session_OnStart and Session_OnEnd procedures.
Currently there are five users.

New user Alice arrives at the site, and a new session is created for her. The Session_OnStart procedure for Alice begins by getting the current value of the Application variable, NumCurrUsers, which is 5. The procedure will then add 1 and update the variable.

However, before the procedure can update the counter variable, user Betty arrives at the site. The same Session_OnStart procedure is begun for Betty, and it retrieves the current value of the Application variable, which is still at 5.

Alice's Session_OnStart procedure completes itself by adding 1 to the value copied from the variable (5), and stores the sum (5+1 = 6) back into the Application variable NumCurrUsers.

Then Betty's Session_OnStart procedure completes itself by adding 1 to the value copied from the variable (Also 5, because it had not yet been updated for Alice), and stores the sum (5+1 = 6) back into the Application variable NumCurrUsers.

We started with 5 users and added 2 more users, but our NumCurrUsers variable is showing a total of 6 users. Our procedure is correct, but an error has been caused by a second occurrence of the procedure beginning before the first was completed.