I´m working on a project using Spring Data-JPA. I need to handle some exceptions in JpaRepository method calls.
In the code bellow, I need to intercept primary key violations erros but I cannot catch the exception directly. In my case, when an exception of this kind occurs, the UnexpectedRollbackException exception is thrown by repository layer (JpaRepository). I need to search inside this exception object to determine what is the cause of the problem.
I am wondering if there is a more "elegant" way to achieve this.
public Phone insert(Phone phone) throws BusinessException {
Phone result = null;
try{
result = phoneRepository.save(phone);
}
catch(UnexpectedRollbackException ex){
if((ex.getCause() != null && ex.getCause() instanceof RollbackException) &&
(ex.getCause().getCause() != null && ex.getCause().getCause() instanceof PersistenceException) &&
(ex.getCause().getCause().getCause() != null && ex.getCause().getCause().getCause() instanceof ConstraintViolationException)){
throw new BusinessException("constraint violation", ex);
}
}
catch(Exception ex){
throw new OuvidorNegocioException("unknown error", ex);
}
return result;
}
Thanks!
UPDATE:
The code bellow seems to be much better.
public Phone insert(Phone phone) throws BusinessException {
Phone result = null;
try{
result = phoneRepository.save(phone);
}
catch(UnexpectedRollbackException ex){
if(ex.getMostSpecificCause() instanceof SQLIntegrityConstraintViolationException){
throw new BusinessException("constraint violation", ex);
}
}
catch(Exception ex){
throw new OuvidorNegocioException("unknown error", ex);
}
return result;
}
Spring MVC provides exception handling for your web application to make sure you are sending your own exception page instead of the server-generated exception to the user. The @ExceptionHandler annotation is used to detect certain runtime exceptions and send responses according to the exception.
Exception HandlerThe @ExceptionHandler is an annotation used to handle the specific exceptions and sending the custom responses to the client. Define a class that extends the RuntimeException class. You can define the @ExceptionHandler method to handle the exceptions as shown.
Exception Handling in Spring Boot helps to deal with errors and exceptions present in APIs so as to deliver a robust enterprise application. This article covers various ways in which exceptions can be handled in a Spring Boot Project. Let's do the initial setup to explore each approach in more depth.
As with others who have commented on your question — I, too, think the solution you have in your post is a bad one. The sole purpose of a repository abstraction is to hide away the persistence mechanism so that clients don't have to know about it. In your case you look for a low-level SQL Exception which has been intentionally abstracted away by Spring as well as JPA.
If you really want to handle a constraint violation, the DataIntegrityViolationException
is the correct one to catch. The repositories will wrap the store specific (MongoDB, JPA, whatever you use) in Spring DataAccessException
subtypes to allow the clients to do exactly that: catch an exception per error type, not per persistence mechanism.
Wherever you handle the exception, you have the option of looking into the getMostSpecificCause()
or getRootCause()
methods of UnexpectedRollbackException
. Here is information about those methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With