Codelock for unique counter

I’m using a profile doc to hold a counter. When I open a new doc, I reference the profile doc to get the new number, and then increment and save the newly updated profile doc. I’ve had problems in the past where the number is duplicated. I want to beable to lock the document to prevent duplicates, and want to know if the createlock/codelock functions will work, and if so, how are they used? The help section isn’t real thorough, so any help would be appreciated.

Subject: Codelock for unique counter

Hi there, it’s just a thought.I’m trying it in my counter on the website(http://lotusrnext.net).

Will let you know if it works or not.

Sub CountItWithLock(page As NotesDocument)

Dim CounterNo As Variant

Dim Status As Integer

Dim LockID As Integer

Dim Delay As Integer



'  Creating a Lock ID or getting the Lock ID 

'  Based on page UniqueID for Page Hits

LockID = Createlock(page.UniversalID)



'Initial Delay

Delay = 1



'  Infinite loop that can only be exited

'  when this agent has a successfull

'  lock or running out of time.  

'  An unsuccessfull lock means

'  that this agent is presently being

'  run by someone else.

Do While True

	If Codelock(LockID) Or Delay > 1000000 Then

		'  We finally have a lock or out of time, exiting Loop

		Exit Do

	End If

	Delay = Delay + 1

Loop



If page.hasitem("wCounter") Then

	CounterNo = page.wCounter(0)

	If Isnull(CounterNo) Or CounterNo="" Then

		CounterNo = 0

	Else

		CounterNo = Cdbl(CounterNo)

	End If

	

	page.wCounter = CounterNo + 1

Else

	Call page.AppendItemValue("wCounter",1)

End If

Call page.save(True,False,True)



'  Once completed, release and

'  destroy this lock.

Status = Codeunlock(LockID)

Status = Destroylock(LockID)

End Sub

Subject: Codelock for unique counter

We have only recently updated from version 5 so I have not used these functions but have had this problem in the past when more than one person creates a document at the same time.If you do not work in a multi national organisation where the database is being used 24/7 you could write an agent that will update the counters overnight when you know that no new documents will be created.

Subject: RE: Codelock for unique counter

That won’t be of any help. The duplicates occur when users are using the db. If one person creates a doc, and a second person creates a doc at the same time, before the counter gets incremented, a duplicate is produced. Incrementing the counter is the first thing I do, but still have problems.

Subject: RE: Codelock for unique counter

Using an agent that runs overnight will prevent duplicate numbers but will mean that the counter will not be created when the document is created or saved.

e.g.

The field on the form that looks up the profile document and stores the counter is called RefNo. Set this to computed with a value of RefNo. This will then be blank when a new document is created.

Create two views in the database

(Ref No) - SELECT Form = “WHATEVER” & RefNo != “”

Sort the first column of the view by RefNo so the latest counter will be the last document in the view

(No Ref No) - SELECT Form = “WHATEVER” & RefNo = “”

Sort the first column of the view by the date the document was created. This view will hold all documents that have been created that day that need reference numbers allocated.

In your agent get a handle on the last document in the (Ref No) view, add one to its RefNo to get your starting number and then loop through the documents in the (No Ref No) view, setting their RefNo and saving.

Doing it this way will ensure that duplicate numbers are not created and there is no need for a profile document. Of course if the users of the database need to know a reference number straight away then this will be no good to you

Subject: RE: Codelock for unique counter

I see where you’re coming from now. The only thing is I need the number upon creating the doc, it’s a tracking number for the documents, so it needs to be there instantaneously.Thanks for inquiring.

Subject: Codelock for unique counter

Please search - this has been address many times before.

Profile documents are cached by the client. If I change a profile document, you won’t see the change.

They are supposed to be used to static information, not dynamic information.

Subject: RE: Codelock for unique counter

Graham, I understand what you are saying about the caching. What is the effect if this mechanism is being used only as a web app? In other words, I have a single database, no replication, and I am using a profile document for my sequential numbering. Does the caching issue have a similar impact here, or is it “safer” since it is the users are all web only?

Subject: RE: Codelock for unique counter

It’s certainly no safer – at least in Notes, a single user will see the changes he/she made in a profile doc during a given session. Not so on the web. The caching there will allow the same user to create duplicates.

If you have to do sequential numbering, use a normal document rather than a profile.