>Hi,
>
>I am working on a multi user system which uses remote views to update tables.I am running the following code to get a unique number from a table, and then update that number with the new uniwue one. when three or more users are running this program at the same time, only one of the users is able to update the number properly, the others get stuck in the loop and get a 'update conflict' error when hitting the tableupdate() code.
>
>
>Any ideas ??
>
>updatedok=.f.
>
>do while updatedok=.f.
> =requery("remote")
> replace remote.no with remote.no+1
>
> updatedok=tableupdate()
> if updatedok=.f.
> =tablerevert()
> unlock all
> endif
>
> loop
>enddo
Given the scenario, I'd recommend not using an RV at all, but directly use the remoted table, and explicitly spin on acquiring a lock, once the lock is acquired, grab the number, increment it, flush the update and then release the lock. Unless there's a demand that all numbers are contiguous and uniformly incremented, allocating a key and simply throwing it away if the update fails is safe and easily serialized:
Create a table IncTable, with two fields:
IncFldName C(20) && can be any size
IncFldValue I && or whatever numeric you need
FUNCTION ReturnIncValue(cIncValueName)
LOCAL nReturnValue, cInAlias
cInAlias = ALIAS()
IF ! USED('IncTable')
USE IncTable IN 0 SHARED AGAIN
ENDIF
SELECT IncTable
LOCATE FOR IncFldName == PADR(cIncValueName,20)
IF ! FOUND()
INSERT INTO IncTable VALUES (cIncValueName,0)
ENDIF
DO WHILE ! RLOCK()
=DOEVENTS()
ENDDO
nReturnValue = IncFldValue
REPLACE IncFldValue WITH nReturnValue + 1
UNLOCK
USE
FLUSH
IF ! EMPTY(cInAlias)
SELECT (cInAlias)
ENDIF
RETURN nReturnValue
This returns a value guarented to be greater than any previously requested value by 1; if you end up discarding the update, just discard the value. The mechanism will support any number of users and any number of named incremental values, and is guarenteed to be serializable.