persistence@glassfish.java.net

Re: How to threat Database exceptions?

From: Wouter van Reeven <wouter_at_van.reeven.nl>
Date: Fri, 18 Dec 2009 12:43:29 +0100

Yes I understand. But you throw the exception in the inserir method so
you need to catch it somewhere else you'll get a stack trace.

On Fri, Dec 18, 2009 at 03:42:30AM -0800, Franzisk wrote:
>
> HorusDAOExceptions is just a simple exception class:
> public class HorusDAOException extends Exception {
>
> public HorusDAOException() {
> }
>
> public HorusDAOException(Throwable e) {
> super(e);
> }
>
> public HorusDAOException(String msg, Throwable e) {
> super(msg, e);
> }
>
> public HorusDAOException(String msg) {
> super(msg);
> }
> }
>
>
> Wouter van Reeven wrote:
> >
> > I guess that depends on how you catch the HorusDAOException. AFAIK you
> > cannot get hold of the database error message. But maybe someone else on
> > this list knows how to do that.
> >
> > On Fri, Dec 18, 2009 at 03:34:43AM -0800, Franzisk wrote:
> >>
> >> My logger is like this:
> >> Logger.getLogger(VendedorUI.class.getName()).log(Level.SEVERE, null, ex);
> >>
> >> But it shows the whole stacktrace in console.
> >>
> >> How could I dot that you mentioned?
> >>
> >>
> >> Wouter van Reeven wrote:
> >> >
> >> > Hi,
> >> >
> >> >
> >> > Inject a logger, catch the exception and log the message?
> >> >
> >> >
> >> > HTH, Wouter
> >> >
> >> > On Fri, Dec 18, 2009 at 03:24:45AM -0800, Franzisk wrote:
> >> >>
> >> >> I need to know how to show to my user just the message sent from the
> >> >> database
> >> >> when persisting, I dont want to the whole stacktrace.
> >> >>
> >> >> This is my method to save:
> >> >> public boolean inserir(T obj) throws HorusDAOException {
> >> >> EntityTransaction transaction = this.manager.getTransaction();
> >> >> transaction.begin();
> >> >> try {
> >> >> this.manager.persist(obj);
> >> >> transaction.commit();
> >> >> } catch (RuntimeException ex) {
> >> >> if (transaction.isActive()) {
> >> >> try {
> >> >> transaction.rollback();
> >> >> } catch (RuntimeException e) {
> >> >> }
> >> >> }
> >> >> throw new HorusDAOException("Error saving:
> >> >> "+ex.getMessage());
> >> >> } finally {
> >> >> manager.close();
> >> >> }
> >> >> return true;
> >> >> }
> >> >>
> >> >> This is the message sent to View:
> >> >> Error saving: Exception [TOPLINK-4002] (Oracle TopLink Essentials -
> >> >> 2.0.1
> >> >> (Build b09d-fcs (12/06/2007))):
> >> >> oracle.toplink.essentials.exceptions.DatabaseException
> >> >> Internal Exception: java.sql.SQLException: Duplicate key or integrity
> >> >> constraint violation message from server: "Duplicate entry
> >> >> '115.393.028-57'
> >> >> for key 'CPF'"
> >> >> Error Code: 1062
> >> >> Call: INSERT INTO VENDEDOR (CALCULAR_SOBRE_VALOR_FRETE, OBSERVACAO,
> >> >> TELEFONE, LOGRADOURO, CPF, NUMERO, PERCENTUAL_COMISSAO, BAIRRO, NOME,
> >> >> CIDADE, RG, ESTADO, EMAIL, COMPLEMENTO, INCLUIR_TAXAS, CEP) VALUES (?,
> >> ?,
> >> >> ?,
> >> >> ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
> >> >> bind => [true, , , , 115.393.028-57, 0, 15.0, , PEDRO, , , SP, ,
> >> null,
> >> >> true, ]
> >> >> Query: InsertObjectQuery([ Cadastro de Vendedor ])
> >> >>
> >> >> I this case I would like to show only this message:
> >> >> "Duplicate entry '115.393.028-57' for key 'CPF'"
> >> >>
> >> >> I'd appreciate very much if someone could help on that.
> >> >> --
> >> >> View this message in context:
> >> >>
> >> http://old.nabble.com/How-to-threat-Database-exceptions--tp26842116p26842116.html
> >> >> Sent from the java.net - glassfish persistence mailing list archive at
> >> >> Nabble.com.
> >> >
> >> > --
> >> > People: "If she weighs the same as a Duck, she's made of wood!"
> >> > Sir Bedevere: "And therefore...?"
> >> > [Monty Python and the Holy Grail]
> >> >
> >> > Skype: wvreeven
> >> > Facebook: wvreeven
> >> > Twitter: wvreeven
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >> http://old.nabble.com/How-to-threat-Database-exceptions--tp26842116p26842191.html
> >> Sent from the java.net - glassfish persistence mailing list archive at
> >> Nabble.com.
> >
> > --
> > There are 10 kinds of people in the world. Those that understand
> > the binary system and those that don't.
> > [Unknown Source]
> >
> > Skype: wvreeven
> > Facebook: wvreeven
> > Twitter: wvreeven
> >
> >
>
> --
> View this message in context: http://old.nabble.com/How-to-threat-Database-exceptions--tp26842116p26842281.html
> Sent from the java.net - glassfish persistence mailing list archive at Nabble.com.

-- 
Oh the sisters of mercy, they are not departed or gone.
They were waiting for me when I thought that I just can't go on.
And they brought me their comfort and later they brought me this song.
Oh I hope you run into them, you who've been travelling so long.
[Leonard Cohen - The Sisters Of Mercy]
Skype: wvreeven
Facebook: wvreeven
Twitter: wvreeven