"be consistent" | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
The proper way to do this without using database dependent extensions like auto_increment, identity columns, etc. is to create a sequence table and access it within a transaction.
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. 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 In reply to Re: portable mysql auto_increment
by mpeppler
|
|