Hello Calvin,
Your GetNewPrimary() code does not guarantee unique PK generation in a heavy multi-user environment. Another weakness is speed...as the newKeyTable grows, it will take longer to get another key (and, it appears this code limits the number of new keys to 100K for all tables in the system).
My suggestion would be to check out the Primary Key article on Craig's site (
www.craigberntson.com). Uniqueness is enforced by an flock() on the keys table
before obtaining the next key value (which I think is clever, Craig).
Incidentally, there is another article on his site about data corruption, which you may find helpful.
I wish you the best in resolving these issues,
steve
>Craig,
>Duh! Oh, PK means primary key. In my defense, I have a severe head cold. Here goes. Primary key code is as follows:
>
>In calling program:
>NewKey=GetNewPrimary()
>replace theprimarykey with NewKey in thisTable
>
>**********************
>Procedure GetNewPrimary
>local newTnumber,nowdbf
>nowdbf=alias()
>newTnumber=0
>use misc in 0 again alias ms
>use theTable again in 0 alias NewKeyTable order primarykey
>sele ms
>set reprocess to -1
>set deleted off
>local ix
>newTnumber=ms.mytrans
>sele NewKeyTable
>for ix=1 to 100000
> seek newTnumber
> if found()
> newTNumber=newTnumber+1
> else
> exit
> endif
>next
>sele ms
>if flock()
> repl ms.mytrans with newTnumber+1
> unlock in ms
>endif
>set deleted on
>use in ms
>use in NewKeyTable
>if !empty(nowdbf)
> sele (nowdbf)
>endif
>return newTnumber
>
>
>While this may not be the most elegant code in the world, I do know that if it fails I will get a primary key violation in short order. I know this because originally I did not have the Set Deleted Off statement and my users generated primary key violations all over the place.
>
Steve Gibson