title: How do I catch the constraint violation exception from EclipseLink?
body: <p>I am using EclipseLink in my web application, and I am having a hard time gracefully catching and handling Exceptions it generates.  I see from <a href="https://stackoverflow.com/questions/2519902/elegantly-handling-constraint-violations-in-ejb-jpa-environment/2522643#2522643">this thread</a> what seems to be a similar problem, but I don't see how to work around or fix it.</p>
  
  <p>My code looks like this:</p>
  
  <pre><code>public void persist(Category category) {
      try {
          utx.begin();
          em.persist(category);
          utx.commit();
      } catch (RollbackException ex) {
             // Log something
      } catch (HeuristicMixedException ex) {
             // Log something
      } catch (HeuristicRollbackException ex) {
             // Log something
      } catch (SecurityException ex) {
             // Log something
      } catch (IllegalStateException ex) {
             // Log something
      } catch (NotSupportedException ex) {
             // Log something
      } catch (SystemException ex) {
             // Log something
      }
  }
  </code></pre>
  
  <p>When <strong>persist()</strong> is called with an entity that violates a uniqueness constraint, I get an explosion of exceptions that are caught and logged by the container.  </p>
  
  <pre><code>Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504):
    org.eclipse.persistence.exceptions.DatabaseException
  Internal Exception: java.sql.SQLIntegrityConstraintViolationException: The statement
    was aborted because it would have caused a duplicate key value in a unique or 
    primary key constraint or unique index identified by 'SQL110911125638570' 
     defined on 'CATEGORY'.
   Error Code: -1
   (etc)
  </code></pre>
  
  <p>I have tried the following:</p>
  
  <pre><code>    try {
          cc.persist(newCategory);        
      } catch (PersistenceException eee) {
          // Never gets here
          System.out.println("MaintCategory.doNewCateogry(): caught: " + eee);
      } catch (DatabaseException dbe) {
          // Never gets here neither
          System.out.println("MaintCategory.doNewCateogry(): caught: " + dbe);            
      }
  </code></pre>
  
  <p>I realize that using DataBaseException is not portable, but I need to start somewhere.  The exceptions never get caught.  Any suggestions?</p>