Normal Topic Multi-User Key Generation (Read 642 times)
MP
Full Member
***
Offline



Posts: 104
Joined: Sep 3rd, 2007
Multi-User Key Generation
Feb 20th, 2008 at 12:07pm
Print Post Print Post  
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:

Code
Select All
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:
Code
Select All
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.
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Multi-User Key Generation
Reply #1 - Feb 20th, 2008 at 2:13pm
Print Post Print Post  
It's pretty high impact for most of our user base. I wouldn't recommend it as the way everybody should be assigning keys, but if you need that level of collision protection, then OK. BTW, if you only need one of these keys in your application with this level of collision protection, you might consider @Number. It autoincrements as soon as it is called. The limitation on it is that there is only one per application.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
MP
Full Member
***
Offline



Posts: 104
Joined: Sep 3rd, 2007
Re: Multi-User Key Generation
Reply #2 - Feb 20th, 2008 at 5:04pm
Print Post Print Post  
Hammer wrote on Feb 20th, 2008 at 2:13pm:
BTW, if you only need one of these keys in your application with this level of collision protection, you might consider @Number. It autoincrements as soon as it is called. The limitation on it is that there is only one per application.

Assuming @Number is thread-safe on the server side, it is an acceptable alternative.  In my case since the keys are hidden from the user, it doesn't really matter if different databases share the same PK generator.  Aesthetically, @Number rubs me wrong, but it's just another large system bias that I should shake off as I work within Sesame.
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Multi-User Key Generation
Reply #3 - Feb 20th, 2008 at 5:08pm
Print Post Print Post  
MP wrote on Feb 20th, 2008 at 5:04pm:
Assuming @Number is thread-safe on the server side, it is an acceptable alternative.  In my case since the keys are hidden from the user, it doesn't really matter if different databases share the same PK generator.  Aesthetically, @Number rubs me wrong, but it's just another large system bias that I should shake off as I work within Sesame.

It's not much used anymore. It's mainly there for Q&A compatibility. Most people use the GlobalValue method now, but, in this case, it might serve your needs without incurring much overhead.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged