|
Posted by Andy Hassall on 10/22/05 14:09
On 21 Oct 2005 17:58:41 -0700, "siliconmike" <siliconmike@yahoo.com> wrote:
>pseudocode:
>
>function generate_unique_id()
>{
> $id = 10 digit random number;
> make sure that $id does not exist in our table
> if it does, generate a new one
>
> insert $id into our table;
You mention a "table", so I assume this is going into a database.
If so, simply add a unique constraint to the column, and leave the locking to
the database.
Don't check if it exists with a SELECT since that introduces a race condition;
attempt the INSERT, and catch the unique key violation if there is one.
--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Navigation:
[Reply to this message]
|