Skip to main content

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) {
            cause = cause.getCause();
            if (cause instanceof org.hibernate.exception.ConstraintViolationException) {
                // Handle ConstraintViolationException
            }
        }
    } else {
        e.printStackTrace();
    }
}

Step 3: Check for Constraint Violation Exception

Check if the underlying exception is an instance of `ConstraintViolationException`.

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) {
            cause = cause.getCause();
            if (cause instanceof org.hibernate.exception.ConstraintViolationException) {
                // Handle ConstraintViolationException
                logger.info("update_duplicate_response | " + update_response);
            }
        }
    } else {
        e.printStackTrace();
    }
}

Remember

* Catch `javax.ejb.EJBException` to handle the underlying exception.
* Use `e.getCause()` to get the underlying exception.
* Check if the underlying exception is an instance of `ConstraintViolationException`.
* Handle the exception accordingly.


Happy coding.


Comments

Popular posts from this blog

Fix HTTP error code 513 on Wildfly

The Mysterious Case of TIME_WAIT and IDLE Connections Have you ever encountered a network issue where your server is consistently showing a high number of connections in the TIME_WAIT and IDLE states? This phenomenon can be frustrating, especially when it indicates that the connections are not being closed properly by the server or client. In our investigation, we found that the culprit behind this issue was an HTTP error code 513 being sent to clients from servers. This error code indicates that the server is overloaded and cannot handle more requests. Furthermore, the client was logging a socket close event, which meant it was terminating the connection prematurely. To replicate this issue, we used JMeter and found that the max concurrent connection limit was reached, resulting in an HTTP error code 513. The allowed queue was also full, contributing to the problem. So, what are the consequences of this issue? Performance degradation and resource wastage on both servers and clients ca...

Not generating any bean definitions from... because of underlying class loading error: Type .... from Service Module Loader] not found

This error is common with JBoss module dependencies. I fixed this issue by adding jboss-structure.xml to my src project directory and adding the missing module to the dependencies node. An example of the file's content is below: <?xml version="1.0" encoding="UTF-8"?> <jboss-deployment-structure>   <deployment>     <dependencies>         <module name="location.missingdependency.jar" services="export" />     </dependencies>   </deployment> </jboss-deployment-structure> Just to mention that this issue was experienced with the EJB jar. For a normal jar/WAR file, a manifest.ft file will do.

Wildfly EJB Remote Client Exceptions.......

Below are exceptions and the solutions I applied while developing a remote client enterprise application. To start with, enable debug for your application server to see the detail and good luck troubleshooting. Exceptions : Starting with  java.lang.ClassNotFoundException: org.hibernate.collection.internal.Persistent......... Solution : I fixed the exception be adding hibernate Entity Manager Dependencies. Exception: Error javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial Solution : Add JBOSS-CLIENT Dependencies to your project Exception : IllegalStateException: EJBCLIENT000025: No EJB receiver available for handling Solution : Confirm that your EJB module have been deployed. Exception : org.jboss.naming.remote.client.initialcontextfactory wildfly     javax.naming.NamingException: WFLYNAM0027: F...