Skip to main content

Posts

Showing posts from October, 2019

Catch persistence java.sql.sqlintegrityconstraintviolationexception

Handling SQL Integrity Constraint Violation Exceptions This tutorial will guide you through handling `SQLIntegrityConstraintViolationException` when persisting objects that violate database constraints. Step 1: Catch the EJB Exception Catch a `javax.ejb.EJBException` which wraps the underlying exception. try {     // persistence transactions } catch (Exception e) {     if (e instanceof javax.ejb.EJBException) {         logger.debug("Exception instance of javax.ejb.EjbException");     } } Step 2: Get the Underlying Exception Get the underlying exception from the `EJBException` using `e.getCause()`. try {     // persistence transactions } catch (Exception e) {     if (e instanceof javax.ejb.EJBException) {         logger.debug("Exception instance of javax.ejb.EjbException");         Throwable cause = e.getCause();         if (cause != null) {     ...