views:

169

answers:

3

In some asp tutorials, like this, i observe the following pattern:

Application.Lock

'do some things with the application object

Application.Unlock

However, since web pages can have multiple instances, there is an obvious concurrency problem. So my questions are the following:

What if one page tries to lock while the object is already locked?

Is there a way to detect whether the application object is locked?

Is it better to just work on an unlocked application object or does that have other consequences?

What if there is only one action involving the application object? ~Is there a reason to lock/unlock in that case?

+1  A: 

If one page tries to lock the Application object while it is already locked, it will wait until the page holding the lock has released it. This will normally be quick (ASP code should only generally hold the lock for long enough to access the shared object that's stored in Application).

Joe
+1  A: 

There will be consequences if you use the application object unlocked. For example if you want to implement a global counter:-

Application("myCounter") = Application("myCounter") + 1

The above code will at times miscount. This code reads, adds and assigns. If two threads try to perform this at the same time they may read the same value and then subsequently write the same value incrementing myCounter by 1 instead of 2.

Whats needed is to ensure that the second thread can't read myCounter until the second thread has written to it. Hence this is better:-

Application.Lock

Application("myCounter") = Application("myCounter") + 1

Application.Unlock

Of course there are concurrency issues if the lock is held for a long time especially if there are other uses for application which are unaffected by the code holding the lock.

Hence you should avoid a design that would require a long lock on the application.

AnthonyWJones
+2  A: 

The Lock method blocks other clients from modifying the variables stored in the Application object, ensuring that only one client at a time can alter or access the Application variables.

If you do not call the Application.Unlock method explicitly, the server unlocks the locked Application object when the .asp file ends or times out.

A lock on the Application object persists for a very short time because the application object is unlocked when the page completes processing or times out.

If one page locks the application object and a second page tries to do the same while the first page still has it locked, the second page will wait for the first to finish, or until the Server.ScriptTimeout limit is reached.

An example:

<%@ Language="VBScript" %> 
<% 
   Application.Lock  
   Application("PageCalls") = Application("PageCalls") + 1  
   Application("LastCall") = Now()  
   Application.Unlock  
%>  

This page has been called <%= Application("PageCalls") %> times.

In the example above, the Lock method prevents more than one client at a time from accessing the variable PageCalls. If the application had not been locked, two clients could simultaneously try to increment the variable PageCalls.

splattne