ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brandon Goodin <brandon.goo...@gmail.com>
Subject Re: Duplicate Key in Db
Date Wed, 23 Feb 2005 21:54:12 GMT
I agree with larry... don't get nuts on this thing. Simple is better.
This is not an abnormal problem. Using an exception is BAD policy.
Exceptions are generic and meant to be thrown and graceful recovery.
This does not fall into thos categories. Exceptions should not be used
for control flow.  Take a moment to consider the definition of
exception.

ex·cep·tion :
One that is excepted, especially a case that does not conform to a
rule or generalization.

You do have a rule here and the expectation is that you would pass in
a key. The challenge here is that you need to make sure that the key
is unique. An exception would be that your sql statement was malformed
not that your data was malformed. Data is validated... infrastructure
failures are for exceptions. Don't mix the two. You will regret it.

So the question is... Where do i best handle the validation of this
key? You really only have a couple choices. You could write a little
extra java code. You could place this into a stored procedure
(database).

You will have to decide which is best.

Brandon

On Wed, 23 Feb 2005 14:02:13 -0700, Larry Meadors
<larry.meadors@gmail.com> wrote:
> Man guys, this is going down a really ugly path.
> 
> What happens when you get a new driver, and the message changes from
> "Invalid key" to "Duplicate key"? Your app is broken. This path to me
> seems to be fraught with peril. ;-)
> 
> I still say that the stored procedure is the safest, simplest,
> fastest, and most controllable route to take.
> 
> Larry
> 
> 
> On Wed, 23 Feb 2005 14:48:24 -0600, Brent Worden <brent@worden.org> wrote:
> > You might also try to examine the errorCode property of the NestedSQLException or
the wrapped SQLexception.  It should hold a vendor specific code that identifies the type
of database error.
> >
> > ----- Original Message -----
> > From: "Brice Ruth" <bdruth@gmail.com>
> > To: ibatis-user-java@incubator.apache.org, "Tim Christopher" <tim.christopher@gmail.com>
> > Subject: Re: Duplicate Key in Db
> > Date: Wed, 23 Feb 2005 14:32:25 -0600
> >
> > >
> > > I think you need to catch a DaoException, and check its cause. If its
> > > cause is a NestedSqlException, you'll need to interrogate that to
> > > determine what the "real" cause was. In your case, it appears a
> > > generic exception is thrown by the JDBC driver
> > > (com.borland.datastore.driver.SqlState), with a "Runtime Error" -
> > > you'll probably need to parse for "Duplicate key" to determine if
> > > that's what's being thrown.
> > >
> > > I've had to do something similar ... it certainly isn't elegant, at
> > > least not the way I did it! Maybe someone else has a better
> > > suggestion.
> > >
> > >
> > > On Wed, 23 Feb 2005 20:23:23 +0000, Tim Christopher
> > > <tim.christopher@gmail.com> wrote:
> > > > >> You can do that now.  Maybe I'm missing something?
> > > > >> The Ibatis insert() statement throws a SQLException.. ..
> > > >
> > > > Below is the first part of stack dump that is triggers by the failed
> > > > insertion, so should I be catching a DaoException or
> > > > NestedSQLException?...  Or is there anything else I can do?
> > > >
> > > > >>>>>>
> > > > StandardWrapperValve[action]: Servlet.service() for servlet action
> > > > threw exception
> > > > com.ibatis.dao.client.DaoException: Failed to update - id
> > > > [insertModule] - parameterObject [com.socs.dto.Module@b9b8d0].  Cause:
> > > > com.ibatis.common.jdbc.exception.NestedSQLException:
> > > > --- The error occurred in com/socs/persistence/sqlmapdao/sql/Module.xml.
> > > > --- The error occurred while applying a parameter map.
> > > > --- Check the insertModule-InlineParameterMap.
> > > > --- Check the statement (update failed).
> > > > --- Cause: com.borland.datastore.driver.SqlState: Runtime Error: [line
> > > > 1, col 6] Duplicate key value for $UNIQUE$1 sort order in "MODULE".
> > > > Caused by: com.borland.datastore.driver.SqlState: Runtime Error: [line
> > > > 1, col 6] Duplicate key value for $UNIQUE$1 sort order in "MODULE".
> > > > <<<<<<
> > > >
> > > > Tim Christopher
> > > >
> >
> >
>

Mime
View raw message