ASP Synchronously write and read in a single file
Is my first experience with ASP(vbscript) so I need your advice.
O开发者_JAVA技巧k there is a requirement to create a counter for keeping track of the number of registrant of a registration form.
What I thought was to create a function in vbscript (in the ASP file) that reads the counter from a file (count.txt), which resides on the server, and increments it by one.
This works when I test it. BUT will it work (and most important will it work properly) when multiple simultaneous registrations are made? It seems to me like a race condition (if it doesn't completely break it :))
Does anybody have any idea how to work around this problem in these specific technologies? any ideas and thoughts are welcomed.
Note:There is no database..:(
Thanks much in advance!
I'm not so familiar with the classic ASP (and the way the corresponding ISAPI extension processes requests) but the following solution might work.
You may want to synchronize access to the file from multiple threads by using SyncLock statement (since all the clients will be served by the same application instance but by different threads within the running process).
First, declare the global variable:
Application.Lock()
Application("SyncFileLock") = New Object()
Application.Unlock()
Next, use the variable declared above in your SyncLock
statement:
SyncLock Application("SyncFileLock")
' Open the file, increment the counter, close the file
End SyncLock
This will ensure that multiple threads won't be writing to same file at the same time.
精彩评论