This thread is more of a commentary then a question. Currently, I have a need to generate primary key values outside of the context of a form within the database that is using the key (i.e. I create records programatically doing things such as file imports, so I need a mechanism to create keys during the import). I use the following code to generate primary keys:
var vintVehicleID as Int
If @GlobalValue( "keyTableID" ) = "" Then GlobalValue( "keyTableID", "0" )
If @IsBlank( keyTableID ) AND @IsNew Then
{
vintVehicleID = @TONUMBER( @GlobalValue( "keyTableID" ))
keyTableID = keyTableID + 1
txtTableID = keyTableID
GlobalValue( "keyTableID", @STR( keyTableID ))
}
Generally, it works fine. However, my concern is that in multi-user mode, this code is susceptible to concurrence issues. If two different users are attempting to create a new record at the same time, both users could end up with the same key value.
Thus, it appears that the best way to create key values in Sesame is to create a separate database dedicated to the creation of key values. This database would only contain a single record, which would contain all of the key values. Since Sesame respects multi-user locks at the result set level, once a user grabs a lock on the record in the key table, other users will be blocked how. Here's some example code:
while NOT bDone
{
vintResultSet = @XResultSetSearch( @FN, "Keys", SEARCH_MODE_AND, SEARCH_SYNTAX_QA, "!fintKeyRecordID=1")
If @XResultSetLocked( vintResultSet )
{
XResultSetClose( vintResultSet )
//Wait for the other user to release the lock.
Loiter( 1000 )
}
Else
{
vintTableID = @XResultSetValue( vintResultSet, "fintTableID" )
vintTableID = vintTableID + 1
XResultSetValue( vintResultSet, "fintTableID", vintTableID )
XResultSetClose( vintResultSet )
bDone = True
}
}
I'm hoping for feedback regarding whether I'm off-base regarding this line of thinking.