Re: DB uniqueness constraints and dealing with them
Re: DB uniqueness constraints and dealing with them
- Subject: Re: DB uniqueness constraints and dealing with them
- From: Chuck Hill <email@hidden>
- Date: Thu, 3 Jul 2008 21:53:20 -0700
On Jul 3, 2008, at 9:47 PM, Lachlan Deck wrote:
On 04/07/2008, at 1:01 PM, Florijan Stamenkovic wrote:
The save -> error check is exactly what I had in mind, sorry if I
was not clear about it. However, I would rather like to delete the
newly saved EO, and throw a validation exception. But this is only
possible if I can find the exactly same EO, once before it saved to
the db, and once after. Is that possible? Huh, never thought about
this.
You'll need the pk to fetch on. Do you need to set a flag that says
'pendingUniquenessCheck' so that it's only available to be read from
other contexts when pending is false.... yada yada.
To answer Q's question, yes, it is a race condition, but a rather
funky one. Because each thread, regardless in which app instance or
even which machine they are one, checks for the record uniqueness
after it has saved it. So, it can happen that there are two records
saved that violate the uniqueness, but it will be detected. And the
EO created in the thread that detected the uniqueness violation
should be deleted. As far as I can see, this can never result in
having the uniqueness violated. In some circumstances however it
could result in all attempts failing. That would be the race
condition in this setup... If both processes manage to save at
first, then the first process detects the violation, but the second
process kicks in before the first deletes it's EO, then the second
one will also detect a violation. Both fail. See what I mean?
But perhaps I do not see it clearly, and somehow uniqueness could
be violated? I've been trying to run multithreaded (non
synchronized) scenarios in my head, and I can never come to one in
which the uniqueness is violated and not detected.
This is really something that can lead to chasing one's tail in the
absence of database-level unique constraints.
e.g.,
- what happens if your application dies whilst checking whether or
not the uniqueness is violated post save?
- what happens if you have multiple instances running and before the
uniqueness is fixed another instance tries to
EOUtilities.objectFor... they'll get an exception.
Been there. Done that. Listen to Lachlan. Database constraints are
the _only_ answer. Everything else a just a problem waiting to happen.
Chuck
--
Practical WebObjects - for developers who want to increase their
overall knowledge of WebObjects or who are trying to solve specific
problems.
http://www.global-village.net/products/practical_webobjects
_______________________________________________
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