Dealing with SQLException with spring,hibernate & Postgres
Posted
by mad
on Stack Overflow
See other posts from Stack Overflow
or by mad
Published on 2010-04-13T21:38:48Z
Indexed on
2010/04/13
21:43 UTC
Read the original article
Hit count: 257
spring
Hi im working on a project using HibernateDaoSUpport from my Daos from Spring & spring-ws & hibernate & postgres who will be used in a national application (means a lot of users)
Actually, every exception from hibernate is automatically transformed into some specific Spring dataAccesException.
I have a table with a keyword on the dabatase & a unique constraint on the keywords : no duplicate keywords is allowed.
I have found twows ways to deal with with that in the Insert Dao: 1- Check for the duplicate manually (with a select) prior to doing your insert. I means that the spring transaction will have a SERIALIZABLE isolation level. The obvious drawback is that we have now 2 queries for a simple insert.Advantage: independent of the database
2-let the insert gone & catch the SqlException & convert it to a userfriendly message & errorcode to the final consumer of our webservices.
Solution 2: Spring has developped a way to translate specific exeptions into customized exceptions. see http://www.oracle.com/technology/pub/articles/marx_spring.html
In my case i would have a ConstraintViolationException.
Ideally i would like to write a custom SQLExceptionTranslator to map the duplicate word constraint in the database with a DuplicateWordException.
But i can have many unique constraints on the same table. So i have to get the message of the SQLEXceptions in order to find the name of the constraint declared in the create table "uq_duplicate-constraint" for example. Now i have a strong dependency with the database.
Thanks in advance for your answers & excuse me for my poor english (it is not my mother tongue)
© Stack Overflow or respective owner