in reply to portable mysql auto_increment
In pseudo code it would look something like this:
The update acquires an exclusive lock on the row which will be held for the duration of the transaction. You then query the table to get the current value of next_key for this sequence. Once that value has been retrieved you commit the transaction, thus releasing the lock on the row.begin transaction update sequence_table set next_key = next_key + 1 where sequence_name = 'somename' select next_key from sequence_table where sequence_name = 'somename' commit transaction
This should (assuming the database engine handles transactions properly) guarantee that each client requesting a sequence value gets a unique value.
With Sybase (or MS-SQL) the whole sequence can be run as a single prepare()/execute() block. For other systems you probably simply want to set AutoCommit to false, do the update and then the query, followed by a $dbh->commit().
Note that this technique will create a hot-spot in your database server if you have a lot of request for new key values.
BTW - you can read a paper on the various key generation techniques for Sybase and/or MS-SQL servers at http://my.sybase.com/detail?id=860
Michael
|
|---|