Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Yet another Unique Key generator
Message
De
20/11/1999 21:20:20
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
00293537
Message ID:
00293578
Vues:
23
>Look again. This is the lines that checks for that condition: if _tally = 0
>
>A key only gets assigned if it can be used to update the counter. If two people get the same value, only one will update the counter, the other will have to try again.
>
>Unless you can show me how it can happen otherwise.

Note my comments beginning with *%%%%


IF !FILE( "test.dbc" )
CREATE DATABASE test
CREATE TABLE KeyCtr ;
( kKeyCtr c(30) PRIMARY KEY, ;
nCtr i )
CREATE TABLE test ;
( kTest i PRIMARY KEY )
* ( kTest i primary key default MkKey( "test.kTest" ) )
CLOSE DATABASE
ENDIF

*%%%% Problem here. Someone else could create the table between the time
*%%%% you check if the file exists and you create it.

USE test!test SHARED
DO WHILE .T.
INSERT INTO test VALUES ( MkKey( "test.kTest" ) )
? test.kTest
ENDDO
RETURN

FUNCTION MkKey( tcFld )

LOCAL llDone

llDone = .F.
DO WHILE !llDone

* Get the current value of the Counter for this key
SELECT nCtr ;
FROM KeyCtr ;
WHERE kKeyCtr = tcFld ;
INTO ARRAY laCtr

IF _TALLY = 0
* If we didn't get one, there is no counter yet.
* So make one and try again
DEBUGOUT "Added Key Counter: " + tcFld

*%%%% Problem here. Someone else could insert the key between the
*%%%% time you do the SELECT and the INSERT
INSERT INTO KeyCtr ( kKeyCtr, nCtr ) VALUES ( tcFld, 0 )
LOOP
ENDIF

* Bump the counter.
* The counter holds the next key value, we use the original value
* The kKeyCtr identifies which counter,
* the nCtr make sure noone else has bumped it allready
UPDATE KeyCtr ;
SET nCtr = laCtr[1] + 1 ;
WHERE kKeyCtr = tcFld AND nCtr = laCtr[1]

IF _TALLY = 0
* The update failed.
* Probably because someone other process has grabbed the same key,
* and bumped the counter first.
* So we will try again.
DEBUGOUT "Counter assignment contention at " + TRANSFORM( laCtr[1] )
* Close the KeyCtr table to assure a fresh version of the key counter data.
* Otherwise, we get the same key from the buffer, not a new one from disk.
USE IN KeyCtr
LOOP
ENDIF

* Once we get to here, we have a good new key.
llDone = .T.

ENDDO

RETURN laCtr[1]
Craig Berntson
MCSD, Microsoft .Net MVP, Grape City Community Influencer
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform