LOTUSSCRIPT LANGUAGE


Extended examples: lock functions
This set of examples demonstrates using the Lock functions to assist in accumulating web site "hits" (counting the number of visits to a web site). The first example demonstrates what happens if several people hit the same web site simultaneously: the users will read the exact same number and the increment will be off.

Example 1:

Sub Initialize

Dim Sess As New NotesSession

Dim Doc As NotesDocument

Dim Count As NotesItem

Set Doc = Sess.SavedData

Set count = Doc.GetFirstItem("WebHits")

If count Is Nothing Then

Set count = New NotesItem(Doc, "WebHits", 0)

End If

count.Values = count.Values(0) + 1

Call Doc.Save(True,False)

End Sub

The second example demonstrates how CodeLock can avoid the problem presented in Example 1. You create and make sure you have a secure lock before you read and make changes to the count, and when you are done, you release the lock.

Example 2:

Sub Initialize

Dim Sess As New NotesSession

Dim Doc As NotesDocument

Dim Count As NotesItem

Dim Status As Integer

Dim LockID As Integer

Dim others As Integer

'  Creating a Lock ID or getting the Lock ID

'  For the event of "WebSiteHits"

LockID = Createlock("WebSiteHits")

'  Infinite loop that can only be exited

'  when this agent has a successfull

'  lock.  An unsuccessfull lock means

'  that this agent is presently being

'  run by someone else.

Do While True

If Codelock(LockID) Then

Exit Do '  We finally have a lock, exiting Loop

End If

Loop

Set Doc = Sess.SavedData

Set count = Doc.GetFirstItem("WebHits")

If count Is Nothing Then

Set count = New NotesItem(Doc, "WebHits", 0)

End If

count.Values = count.Values(0) + 1

Call Doc.Save(True,False)

'  Once completed, release and

'  destroy this lock so another

'  run of this agent can continue.

Status = CodeUnlock(LockID)

Status = DestroyLock(LockID)

End Sub