Re: [SOLVED] Re: DB uniqueness constraints and dealing with them
Re: [SOLVED] Re: DB uniqueness constraints and dealing with them
- Subject: Re: [SOLVED] Re: DB uniqueness constraints and dealing with them
- From: Neil MacLennan <email@hidden>
- Date: Fri, 04 Jul 2008 20:00:40 +0100
In case anyone's building a library here... this is some code for
MySQL uniqueness (with some credit to Practical WebObjects Ch 5). I
think that's Frontbase, Openbase, Postgres and MySQL covered so far...
<snippet>
try {
editingContext.saveChanges();
} catch (EOGeneralAdaptorException e) {
if (isDbDuplicateKeyError(e)) {
log.error("Duplicate Key Found for: " + tx.barcode());
//... more code...
} else {
throw e;
}
} catch (er.extensions.ERXValidationException ve) {
log.error("Validation Error for: " + tx.barcode() + ". Reverting
E-ticket. Not Saved. " + ve.getMessage());
editingContext.revert();
}
</snippet>
// Supporting Method
private boolean isDbDuplicateKeyError(EOGeneralAdaptorException e) {
if
(e
.userInfo().objectForKey(EOAdaptorChannel.FailedAdaptorOperationKey) !
= null) {
EOAdaptorOperation operation =
(EOAdaptorOperation
)e.userInfo().objectForKey(EOAdaptorChannel.FailedAdaptorOperationKey);
if (operation.exception() != null) {
if (operation.exception().getMessage().indexOf("error
code: 1062") >= 0) {
// Duplicate Key was found. 1062 is MySQL's error
code for unique constraint violation
return true;
}
}
}
return false;
}
.neilmac
On 4 Jul 2008, at 16:36, Miguel Arroz wrote:
In PostgreSQL it works. I don't know if it works on other DBs, but
I suppose the error message will contain the constraint name
somehow. Anyway, test it on the DBs you use
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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