Depending on your persistence provider, you may encounter an SQLIntegrityConstraintViolationException when you try to persist an object that violates a database constraint. To handle this exception, you need to find out the class that wraps it by calling the e.getClass() and e.getCause() methods on the exception object.
For example, if you are using Hibernate as your persistence provider, you can catch a javax.ejb.EJBException and then get the org.hibernate.exception.ConstraintViolationException from its cause.
try{
//persistence transactions
}catch (Exception e) {
if (e instanceof javax.ejb.EJBException) {
logger.debug("Exception instance of javax.ejb.EJBException");
Throwable cause = e.getCause(); //persistence exception
if (cause != null) {
cause = cause.getCause(); if (cause instanceof org.hibernate.exception.ConstraintViolationException) {
logger.info("update_duplicate_response | " + update_response);
}
}
} else {
e.printStackTrace();
}
}
Happy coding.
Comments
Post a Comment