Building WebApps  «Prev  

Timing Challenge with Shared Application Variable in ASP

1) Our website is using an application variable to track the current number of users accessing the site at any time.
1) 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.

2) New user Alice arrives at the site, and a new session is created for her.
2) 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.


3) However, before the procedure can update the counter variable, user Betty arrives at the site.
3) 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.

4) Alice's Session_OnStart procedure completes itself by adding 1 to the value copied from the variable (5), and stores the sum
4) 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.


5) Then Betty's Session_OnStart procedure completes itself by adding 1 to the value copied from the variable
5) 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.

6) We started with 5 users and added 2 more users, but our NumCurrUsers variable is showing a total of 6 users.
6) 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.