You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by yuan gogo <go...@gmail.com> on 2007/12/29 06:59:36 UTC

How to custom database exception?

Hi, all.
    I'm new to iBatis, and wonder how to get custom data exception in the
web application.

   Say, I have a database table Users (id, username, password), the username
is UNIQUE.
   While insert new user data with a duplicated username , a data exception
will be thrown from jdbc indicate constraint conflict on some datafileds.
But I personally think throw a custom exception like
UsernameExistedException will be a better way for other layer to log or
notify user.

   How can I do it?

Ps : I use Tapestry 5 + spring 2.5M + iBatis 2 + postgresql 8.3 beta +
tomcat 6.0.14 for evaluation.

Thanks!

gogoyuan [_at_] gmail.com

Re: How to custom database exception?

Posted by Jörg Gottschling <jo...@myndian.de>.
Hello,

> I'm new to iBatis, and wonder how to get custom data exception in 
> the web application.
> [...]
> Ps : I use Tapestry 5 + spring 2.5M + iBatis 2 + postgresql 8.3 beta + 
> tomcat 6.0.14 for evaluation.

As mentioned IBatis can not do this for you and I think it would also be 
the wrong place. You are using Spring. If you use Springs 
SqlMapClientDaoSupport to implement your DAOs the SqlMapClientTemplate 
will translate DBMS-specific Exception into a Spring-specific Exception. 
It think org.springframework.dao.DataIntegrityViolationException will be 
thrown. The Template uses a SQLExceptionTranslator to do that. You could 
implement your own if you like.

This may be an entry point:
<http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/jdbc/support/SQLExceptionTranslator.html>

Greetings and a happy new year,
   Jörg Gottschling

-- 
Jörg Gottschling
web:    http://www.myndian.de
eMail:  joerg@myndian.de
GnuPG:  0x9B1C64BB
ICQ:    177003788


Re: How to custom database exception?

Posted by yuan gogo <go...@gmail.com>.
Thank you!


2007/12/29, Larry Meadors <lm...@apache.org>:
>
> That's a SQL/JDBC problem, really - a unique constraint doesn't give
> you much in terms of customizing the message. I guess if you wanted
> to, you could look at the message and try to map it to a nicer
> message...but iBATIS isn't going to do that for you. It would be
> infeasible to map any database error message to custom exceptions.
>
> Larry
>
>
> On Dec 28, 2007 10:59 PM, yuan gogo <go...@gmail.com> wrote:
> > Hi, all.
> >     I'm new to iBatis, and wonder how to get custom data exception in
> the
> > web application.
> >
> >    Say, I have a database table Users (id, username, password), the
> username
> > is UNIQUE.
> >    While insert new user data with a duplicated username , a data
> exception
> > will be thrown from jdbc indicate constraint conflict on some
> datafileds.
> > But I personally think throw a custom exception like
> > UsernameExistedException will be a better way for other layer to log or
> > notify user.
> >
> >    How can I do it?
> >
> > Ps : I use Tapestry 5 + spring 2.5M + iBatis 2 + postgresql 8.3 beta +
> > tomcat 6.0.14 for evaluation.
> >
> > Thanks!
> >
> > gogoyuan [_at_] gmail.com
> >
>

RE: How to custom database exception?

Posted by "Kezerashvili, Denis" <De...@gs.com>.
Well, from the original message it sounded to me that the problem was
that some obscure SQLException was thrown and "someone with an
impressive title had seen that exception in production". I assume the
exception was logged and it was not clear what it was to that particular
person. The code was added to "convert" that obscure SQLException into
something meaningful. 
If there was a need to act on that exception (and seeing Chris' reply
there was such a need), I am not sure why the conversion needed to
happen at all, why not just catch an SQLException at the place where it
needs to be acted upon and maybe log the issue at that point as well
with a clear message of what happened.

But I think we are getting off the original topic here. 

Denis

-----Original Message-----
From: larry.meadors@gmail.com [mailto:larry.meadors@gmail.com] On Behalf
Of Larry Meadors
Sent: Monday, December 31, 2007 11:58 AM
To: user-java@ibatis.apache.org
Subject: Re: How to custom database exception?

I'd have to agree with Chris.

If your code "won't be broken unless you are relying on that generated
custom exception to be caught and acted upon", then what's the point
of the custom exception?

It sounds to me like you are saying "It'll work, unless it doesn't, so
don't rely on it working." :-)

Seems like extra work (cost) without added functionality (value). I
know my clients don't like that. ;-)

Larry


On Dec 31, 2007 9:29 AM, Chris Lamey <cl...@localmatters.com> wrote:
>
>
>
> That's exactly why it was a hack and is easily broken.
>
>  Like I said before, a test in the catch looked for a specific string
in the
> SQLException's message and threw a new custom exception that was
handled
> appropriately elsewhere.
>
>
>
>
>  -----Original Message-----
>  From: Kezerashvili, Denis [mailto:Denis.Kezerashvili@gs.com]
>  Sent: Mon 12/31/2007 6:47 AM
>  To: user-java@ibatis.apache.org
>  Subject: RE: How to custom database exception?
>
>  You code won't be broken unless you are relying on that generated
custom
>  exception to be caught and acted upon. If the Oracle exception has
>  changed for some reason it will just fall through your custom
handling
>  piece of code. I am guessing you are rethrowing other types of
>  exceptions that you did not write special handling for, so you should
be
>  fine:)
>
>
>  Denis
>
>
>  ________________________________
>
>          From: Chris Lamey [mailto:clamey@localmatters.com]
>          Sent: Saturday, December 29, 2007 9:47 AM
>          To: "<user-java"@ibatis.apache.org
>          Subject: RE: How to custom database exception?
>
>
>
>          Hello, my name is Chris and I've committed this most
egregious
>  hack.
>
>          Many years ago when I was young and easily led down the dark
>  side, a requirement came down from on high that required a custom
user
>  message for a specific Oracle error message.  The project was already
>  through the development and test phase.  Someone with an impressive
>  title had seen that exception in production and it had to be taken
care
>  of.  The requirement came at me late at night and had to be
implemented
>  immediately - there was no time to think!
>
>          So in the catch block of SQLException, I tested for the
specific
>  Oracle error ID in the exception mesage.  If it was there, I threw
one
>  of our custom exceptions that produced the desired effect in the
>  application.
>
>          At the time, I commented that code as something that was a
hack
>  and had to be changed.  I promised myself I would go back and fix it.
>  But alas, this did not come to pass and that code is still running.
>
>          It was to easy to get done at the time...it haunts me to this
>  day.
>
>          If they switch DBs, even versions of Oracle, that code is
easily
>  broken.  Trying to map DB specific error codes to an Exception
hierarchy
>  is madness.  Instead, it is better to test for those conditions and
>  constraints up front and not to let it be an error condition.
>
>          It's also good to read Josh Bloch's Effective Java for the
>  section on Exception handling.  He makes a pretty strong case for not
>  using custom Exceptions as what amounts to return codes for commonly
>  expected cases.
>
>          Cheers,
>          Chris
>
>          -----Original Message-----
>          From: larry.meadors@gmail.com on behalf of Larry Meadors
>          Sent: Sat 12/29/2007 6:35 AM
>          To: user-java@ibatis.apache.org
>          Subject: Re: How to custom database exception?
>
>          That's a SQL/JDBC problem, really - a unique constraint
doesn't
>  give
>          you much in terms of customizing the message. I guess if you
>  wanted
>          to, you could look at the message and try to map it to a
nicer
>          message...but iBATIS isn't going to do that for you. It would
be
>          infeasible to map any database error message to custom
>  exceptions.
>
>          Larry
>
>
>          On Dec 28, 2007 10:59 PM, yuan gogo <go...@gmail.com>
wrote:
>          > Hi, all.
>          >     I'm new to iBatis, and wonder how to get custom data
>  exception in the
>          > web application.
>          >
>          >    Say, I have a database table Users (id, username,
>  password), the username
>          > is UNIQUE.
>          >    While insert new user data with a duplicated username ,
a
>  data exception
>          > will be thrown from jdbc indicate constraint conflict on
some
>  datafileds.
>          > But I personally think throw a custom exception like
>          > UsernameExistedException will be a better way for other
layer
>  to log or
>          > notify user.
>          >
>          >    How can I do it?
>          >
>          > Ps : I use Tapestry 5 + spring 2.5M + iBatis 2 + postgresql
>  8.3 beta +
>          > tomcat 6.0.14 for evaluation.
>          >
>          > Thanks!
>          >
>          > gogoyuan [_at_] gmail.com
>          >
>
>
>
>
>
>
>

Re: How to custom database exception?

Posted by Larry Meadors <lm...@apache.org>.
I'd have to agree with Chris.

If your code "won't be broken unless you are relying on that generated
custom exception to be caught and acted upon", then what's the point
of the custom exception?

It sounds to me like you are saying "It'll work, unless it doesn't, so
don't rely on it working." :-)

Seems like extra work (cost) without added functionality (value). I
know my clients don't like that. ;-)

Larry


On Dec 31, 2007 9:29 AM, Chris Lamey <cl...@localmatters.com> wrote:
>
>
>
> That's exactly why it was a hack and is easily broken.
>
>  Like I said before, a test in the catch looked for a specific string in the
> SQLException's message and threw a new custom exception that was handled
> appropriately elsewhere.
>
>
>
>
>  -----Original Message-----
>  From: Kezerashvili, Denis [mailto:Denis.Kezerashvili@gs.com]
>  Sent: Mon 12/31/2007 6:47 AM
>  To: user-java@ibatis.apache.org
>  Subject: RE: How to custom database exception?
>
>  You code won't be broken unless you are relying on that generated custom
>  exception to be caught and acted upon. If the Oracle exception has
>  changed for some reason it will just fall through your custom handling
>  piece of code. I am guessing you are rethrowing other types of
>  exceptions that you did not write special handling for, so you should be
>  fine:)
>
>
>  Denis
>
>
>  ________________________________
>
>          From: Chris Lamey [mailto:clamey@localmatters.com]
>          Sent: Saturday, December 29, 2007 9:47 AM
>          To: "<user-java"@ibatis.apache.org
>          Subject: RE: How to custom database exception?
>
>
>
>          Hello, my name is Chris and I've committed this most egregious
>  hack.
>
>          Many years ago when I was young and easily led down the dark
>  side, a requirement came down from on high that required a custom user
>  message for a specific Oracle error message.  The project was already
>  through the development and test phase.  Someone with an impressive
>  title had seen that exception in production and it had to be taken care
>  of.  The requirement came at me late at night and had to be implemented
>  immediately - there was no time to think!
>
>          So in the catch block of SQLException, I tested for the specific
>  Oracle error ID in the exception mesage.  If it was there, I threw one
>  of our custom exceptions that produced the desired effect in the
>  application.
>
>          At the time, I commented that code as something that was a hack
>  and had to be changed.  I promised myself I would go back and fix it.
>  But alas, this did not come to pass and that code is still running.
>
>          It was to easy to get done at the time...it haunts me to this
>  day.
>
>          If they switch DBs, even versions of Oracle, that code is easily
>  broken.  Trying to map DB specific error codes to an Exception hierarchy
>  is madness.  Instead, it is better to test for those conditions and
>  constraints up front and not to let it be an error condition.
>
>          It's also good to read Josh Bloch's Effective Java for the
>  section on Exception handling.  He makes a pretty strong case for not
>  using custom Exceptions as what amounts to return codes for commonly
>  expected cases.
>
>          Cheers,
>          Chris
>
>          -----Original Message-----
>          From: larry.meadors@gmail.com on behalf of Larry Meadors
>          Sent: Sat 12/29/2007 6:35 AM
>          To: user-java@ibatis.apache.org
>          Subject: Re: How to custom database exception?
>
>          That's a SQL/JDBC problem, really - a unique constraint doesn't
>  give
>          you much in terms of customizing the message. I guess if you
>  wanted
>          to, you could look at the message and try to map it to a nicer
>          message...but iBATIS isn't going to do that for you. It would be
>          infeasible to map any database error message to custom
>  exceptions.
>
>          Larry
>
>
>          On Dec 28, 2007 10:59 PM, yuan gogo <go...@gmail.com> wrote:
>          > Hi, all.
>          >     I'm new to iBatis, and wonder how to get custom data
>  exception in the
>          > web application.
>          >
>          >    Say, I have a database table Users (id, username,
>  password), the username
>          > is UNIQUE.
>          >    While insert new user data with a duplicated username , a
>  data exception
>          > will be thrown from jdbc indicate constraint conflict on some
>  datafileds.
>          > But I personally think throw a custom exception like
>          > UsernameExistedException will be a better way for other layer
>  to log or
>          > notify user.
>          >
>          >    How can I do it?
>          >
>          > Ps : I use Tapestry 5 + spring 2.5M + iBatis 2 + postgresql
>  8.3 beta +
>          > tomcat 6.0.14 for evaluation.
>          >
>          > Thanks!
>          >
>          > gogoyuan [_at_] gmail.com
>          >
>
>
>
>
>
>
>

RE: How to custom database exception?

Posted by Chris Lamey <cl...@localmatters.com>.
That's exactly why it was a hack and is easily broken.

Like I said before, a test in the catch looked for a specific string in the SQLException's message and threw a new custom exception that was handled appropriately elsewhere.


-----Original Message-----
From: Kezerashvili, Denis [mailto:Denis.Kezerashvili@gs.com]
Sent: Mon 12/31/2007 6:47 AM
To: user-java@ibatis.apache.org
Subject: RE: How to custom database exception?
 
You code won't be broken unless you are relying on that generated custom
exception to be caught and acted upon. If the Oracle exception has
changed for some reason it will just fall through your custom handling
piece of code. I am guessing you are rethrowing other types of
exceptions that you did not write special handling for, so you should be
fine:)
 
 
Denis


________________________________

	From: Chris Lamey [mailto:clamey@localmatters.com] 
	Sent: Saturday, December 29, 2007 9:47 AM
	To: "<user-java"@ibatis.apache.org
	Subject: RE: How to custom database exception?
	
	

	Hello, my name is Chris and I've committed this most egregious
hack.
	
	Many years ago when I was young and easily led down the dark
side, a requirement came down from on high that required a custom user
message for a specific Oracle error message.  The project was already
through the development and test phase.  Someone with an impressive
title had seen that exception in production and it had to be taken care
of.  The requirement came at me late at night and had to be implemented
immediately - there was no time to think!
	
	So in the catch block of SQLException, I tested for the specific
Oracle error ID in the exception mesage.  If it was there, I threw one
of our custom exceptions that produced the desired effect in the
application.
	
	At the time, I commented that code as something that was a hack
and had to be changed.  I promised myself I would go back and fix it.
But alas, this did not come to pass and that code is still running.
	
	It was to easy to get done at the time...it haunts me to this
day.
	
	If they switch DBs, even versions of Oracle, that code is easily
broken.  Trying to map DB specific error codes to an Exception hierarchy
is madness.  Instead, it is better to test for those conditions and
constraints up front and not to let it be an error condition.
	
	It's also good to read Josh Bloch's Effective Java for the
section on Exception handling.  He makes a pretty strong case for not
using custom Exceptions as what amounts to return codes for commonly
expected cases.
	
	Cheers,
	Chris
	
	-----Original Message-----
	From: larry.meadors@gmail.com on behalf of Larry Meadors
	Sent: Sat 12/29/2007 6:35 AM
	To: user-java@ibatis.apache.org
	Subject: Re: How to custom database exception?
	
	That's a SQL/JDBC problem, really - a unique constraint doesn't
give
	you much in terms of customizing the message. I guess if you
wanted
	to, you could look at the message and try to map it to a nicer
	message...but iBATIS isn't going to do that for you. It would be
	infeasible to map any database error message to custom
exceptions.
	
	Larry
	
	
	On Dec 28, 2007 10:59 PM, yuan gogo <go...@gmail.com> wrote:
	> Hi, all.
	>     I'm new to iBatis, and wonder how to get custom data
exception in the
	> web application.
	>
	>    Say, I have a database table Users (id, username,
password), the username
	> is UNIQUE.
	>    While insert new user data with a duplicated username , a
data exception
	> will be thrown from jdbc indicate constraint conflict on some
datafileds.
	> But I personally think throw a custom exception like
	> UsernameExistedException will be a better way for other layer
to log or
	> notify user.
	>
	>    How can I do it?
	>
	> Ps : I use Tapestry 5 + spring 2.5M + iBatis 2 + postgresql
8.3 beta +
	> tomcat 6.0.14 for evaluation.
	>
	> Thanks!
	>
	> gogoyuan [_at_] gmail.com
	>
	
	





RE: How to custom database exception?

Posted by "Kezerashvili, Denis" <De...@gs.com>.
You code won't be broken unless you are relying on that generated custom
exception to be caught and acted upon. If the Oracle exception has
changed for some reason it will just fall through your custom handling
piece of code. I am guessing you are rethrowing other types of
exceptions that you did not write special handling for, so you should be
fine:)
 
 
Denis


________________________________

	From: Chris Lamey [mailto:clamey@localmatters.com] 
	Sent: Saturday, December 29, 2007 9:47 AM
	To: "<user-java"@ibatis.apache.org
	Subject: RE: How to custom database exception?
	
	

	Hello, my name is Chris and I've committed this most egregious
hack.
	
	Many years ago when I was young and easily led down the dark
side, a requirement came down from on high that required a custom user
message for a specific Oracle error message.  The project was already
through the development and test phase.  Someone with an impressive
title had seen that exception in production and it had to be taken care
of.  The requirement came at me late at night and had to be implemented
immediately - there was no time to think!
	
	So in the catch block of SQLException, I tested for the specific
Oracle error ID in the exception mesage.  If it was there, I threw one
of our custom exceptions that produced the desired effect in the
application.
	
	At the time, I commented that code as something that was a hack
and had to be changed.  I promised myself I would go back and fix it.
But alas, this did not come to pass and that code is still running.
	
	It was to easy to get done at the time...it haunts me to this
day.
	
	If they switch DBs, even versions of Oracle, that code is easily
broken.  Trying to map DB specific error codes to an Exception hierarchy
is madness.  Instead, it is better to test for those conditions and
constraints up front and not to let it be an error condition.
	
	It's also good to read Josh Bloch's Effective Java for the
section on Exception handling.  He makes a pretty strong case for not
using custom Exceptions as what amounts to return codes for commonly
expected cases.
	
	Cheers,
	Chris
	
	-----Original Message-----
	From: larry.meadors@gmail.com on behalf of Larry Meadors
	Sent: Sat 12/29/2007 6:35 AM
	To: user-java@ibatis.apache.org
	Subject: Re: How to custom database exception?
	
	That's a SQL/JDBC problem, really - a unique constraint doesn't
give
	you much in terms of customizing the message. I guess if you
wanted
	to, you could look at the message and try to map it to a nicer
	message...but iBATIS isn't going to do that for you. It would be
	infeasible to map any database error message to custom
exceptions.
	
	Larry
	
	
	On Dec 28, 2007 10:59 PM, yuan gogo <go...@gmail.com> wrote:
	> Hi, all.
	>     I'm new to iBatis, and wonder how to get custom data
exception in the
	> web application.
	>
	>    Say, I have a database table Users (id, username,
password), the username
	> is UNIQUE.
	>    While insert new user data with a duplicated username , a
data exception
	> will be thrown from jdbc indicate constraint conflict on some
datafileds.
	> But I personally think throw a custom exception like
	> UsernameExistedException will be a better way for other layer
to log or
	> notify user.
	>
	>    How can I do it?
	>
	> Ps : I use Tapestry 5 + spring 2.5M + iBatis 2 + postgresql
8.3 beta +
	> tomcat 6.0.14 for evaluation.
	>
	> Thanks!
	>
	> gogoyuan [_at_] gmail.com
	>
	
	


RE: How to custom database exception?

Posted by Chris Lamey <cl...@localmatters.com>.
Hello, my name is Chris and I've committed this most egregious hack.

Many years ago when I was young and easily led down the dark side, a requirement came down from on high that required a custom user message for a specific Oracle error message.  The project was already through the development and test phase.  Someone with an impressive title had seen that exception in production and it had to be taken care of.  The requirement came at me late at night and had to be implemented immediately - there was no time to think!

So in the catch block of SQLException, I tested for the specific Oracle error ID in the exception mesage.  If it was there, I threw one of our custom exceptions that produced the desired effect in the application.

At the time, I commented that code as something that was a hack and had to be changed.  I promised myself I would go back and fix it.  But alas, this did not come to pass and that code is still running.

It was to easy to get done at the time...it haunts me to this day.

If they switch DBs, even versions of Oracle, that code is easily broken.  Trying to map DB specific error codes to an Exception hierarchy is madness.  Instead, it is better to test for those conditions and constraints up front and not to let it be an error condition.

It's also good to read Josh Bloch's Effective Java for the section on Exception handling.  He makes a pretty strong case for not using custom Exceptions as what amounts to return codes for commonly expected cases.

Cheers,
Chris

-----Original Message-----
From: larry.meadors@gmail.com on behalf of Larry Meadors
Sent: Sat 12/29/2007 6:35 AM
To: user-java@ibatis.apache.org
Subject: Re: How to custom database exception?
 
That's a SQL/JDBC problem, really - a unique constraint doesn't give
you much in terms of customizing the message. I guess if you wanted
to, you could look at the message and try to map it to a nicer
message...but iBATIS isn't going to do that for you. It would be
infeasible to map any database error message to custom exceptions.

Larry


On Dec 28, 2007 10:59 PM, yuan gogo <go...@gmail.com> wrote:
> Hi, all.
>     I'm new to iBatis, and wonder how to get custom data exception in the
> web application.
>
>    Say, I have a database table Users (id, username, password), the username
> is UNIQUE.
>    While insert new user data with a duplicated username , a data exception
> will be thrown from jdbc indicate constraint conflict on some datafileds.
> But I personally think throw a custom exception like
> UsernameExistedException will be a better way for other layer to log or
> notify user.
>
>    How can I do it?
>
> Ps : I use Tapestry 5 + spring 2.5M + iBatis 2 + postgresql 8.3 beta +
> tomcat 6.0.14 for evaluation.
>
> Thanks!
>
> gogoyuan [_at_] gmail.com
>


Re: How to custom database exception?

Posted by Larry Meadors <lm...@apache.org>.
That's a SQL/JDBC problem, really - a unique constraint doesn't give
you much in terms of customizing the message. I guess if you wanted
to, you could look at the message and try to map it to a nicer
message...but iBATIS isn't going to do that for you. It would be
infeasible to map any database error message to custom exceptions.

Larry


On Dec 28, 2007 10:59 PM, yuan gogo <go...@gmail.com> wrote:
> Hi, all.
>     I'm new to iBatis, and wonder how to get custom data exception in the
> web application.
>
>    Say, I have a database table Users (id, username, password), the username
> is UNIQUE.
>    While insert new user data with a duplicated username , a data exception
> will be thrown from jdbc indicate constraint conflict on some datafileds.
> But I personally think throw a custom exception like
> UsernameExistedException will be a better way for other layer to log or
> notify user.
>
>    How can I do it?
>
> Ps : I use Tapestry 5 + spring 2.5M + iBatis 2 + postgresql 8.3 beta +
> tomcat 6.0.14 for evaluation.
>
> Thanks!
>
> gogoyuan [_at_] gmail.com
>