persistence@glassfish.java.net

How to threat Database exceptions?

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

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.