Unique Fields in Database
Unique Fields in Database
- Subject: Unique Fields in Database
- From: Neil MacLennan <email@hidden>
- Date: Mon, 21 Jan 2008 02:06:20 +0000
Following on from the postings on subject, "Avoiding duplicate
records" can I get my understanding right? (so that I don't waste any
more time trying to fix what can't be fixed):
I'm looking for, say, a six-character unique field in my database
record. I generate this randomly so that each is unpredictable (at
least within 36^6 guesses).
As I understand it, there's no way to:
i) generate the random character string,
ii) check for uniqueness in the database
iii) insert it into the database with the guarantee that no other
instance has co-incidentally inserted the same string in the
milliseconds between ii) and iii)
Currently I have a stored procedure (in MySQL) finding me a free
random string (I figure that this will be quicker than WO trying to
find me one) and then inserting that String into my EO and saving it.
I then look for a EOGeneralAdaptorException on saveChanges() and parse
it for MySQL's error code for failing a UNIQUE constraint on the DB
(error code: 1062). If an exception is thrown, I go back an look for
another free string from the stored procedure. I do this in a loop
repeating 50 times, at which point I give up and throw the Exception
right up to the user interface level as a "problem".
I'll only be using 0.1% of the available key space for a 6-character
alpha-num string, so I figure that a loop of 50 will be sufficient to
always find me an available string. But, in the general case, it just
seems "undesirable" as a solution.
Is this the best I can hope for?
.neilmac
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden