persistence@glassfish.java.net

Re: How to threat Database exceptions?

From: Franzisk <francisco.vieira.souza_at_gmail.com>
Date: Fri, 18 Dec 2009 03:42:30 -0800 (PST)

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.