Results 1 to 4 of 4
Thread: Exception handling REST and EJB
- 09-06-2018, 04:58 PM #1
Exception handling REST and EJB
Hi all,
What is the best practice for handling exceptions with the following frameworks:
Browser <--> REST API (JAX-RS, using RESTEasy) <--> EJB <--> JPA (using Hibernate) <--> Database (using MariaDB)
Assume I have a unique constraint on a column. When the browser posts a new record in that table, the request trickles down the stack and causes a javax.persistence.PersistenceException in my EJB, caused by Hibernate that puked with a org.hibernate.exception.ConstraintViolationExcepti on. I don't want to mix Hibernate code in my REST API service classes so I can't throw it to the calling object, nor should I push the whole stacktrace to the REST service. So what DO I tell the REST service class? I can make an EntityException, but how would I make sure the error message is what it should be? In this case, I am looking for "Duplicate entry something". But that would require me to loop over the stacktrace looking for specific SQLExceptions. That feels counter-intuitive and unnecessary.
These are the stacktrace causes:
javax.persistence.PersistenceException
org.hibernate.exception.ConstraintViolationExcepti on: could not execute statement
java.sql.SQLIntegrityConstraintViolationException: (conn=31) Duplicate entry 'AAA' for key 'accounts_ix1'
java.sql.SQLException: Duplicate entry 'AAA' for key 'accounts_ix1'
What does your Exception handling code look like when using JPA/Hibernate and (possibly) REST classes?"It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013
- 09-25-2018, 03:25 AM #2
Senior Member
- Join Date
- Sep 2014
- Location
- MA, USA
- Posts
- 399
- Rep Power
- 7
Re: Exception handling REST and EJB
I would try .... catch(HibernateException ...) {... and then myTransaction.rollback()}. That obviously does not resolve the accident in the primary key of your table (how did anyone manage to enter a duplicate key there in the first place???). The latter question seems to be the challenge here.
- 09-25-2018, 11:19 AM #3
Re: Exception handling REST and EJB
Thanks for your reply, Benji. Much appreciated.
I don't like catching vendor specific exceptions, since I want to keep this as pure JEE as I can. So I am trying to avoid catching any Hibernate*Exception. And running rollback() on a container managed transaction is not going to work. I can't even start a transaction if my container and EntityManager are calling the shots.
I agree on duplicate primary keys is questionable (one should never create id's themselves :)), but in this case, the duplicate entry key is not about primary keys, it's about violating the unique constraint that's on the table. That's entirely possible when a user enters data.
You can pull my question in a wider context about how to handle any exception in JPA→EJB→REST service.Last edited by SurfMan; 09-25-2018 at 11:46 AM.
"It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013
- 09-25-2018, 02:54 PM #4
Senior Member
- Join Date
- Sep 2014
- Location
- MA, USA
- Posts
- 399
- Rep Power
- 7
Similar Threads
-
exception handling
By NoobieCode in forum New To JavaReplies: 6Last Post: 09-27-2013, 01:52 PM -
Exception handling
By Kartiky14 in forum New To JavaReplies: 3Last Post: 03-25-2012, 09:07 AM -
Exception Handling
By link6790 in forum New To JavaReplies: 16Last Post: 05-19-2011, 07:57 PM -
Exception Handling
By eLancaster in forum New To JavaReplies: 4Last Post: 02-20-2011, 01:00 AM -
Exception Handling...
By focus_nitin in forum New To JavaReplies: 1Last Post: 02-16-2008, 04:13 AM
Bookmarks