Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 323380

Summary: Cannot catch any database related exceptions (DatabaseException or a PersistenceException such as EntityExistsException)
Product: z_Archived Reporter: Andreas Steinwachs <andreas>
Component: EclipselinkAssignee: Nobody - feel free to take it <nobody>
Status: CLOSED INVALID QA Contact:
Severity: normal    
Priority: P3 CC: andreas, tom.ware
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Attachments:
Description Flags
The test case
none
The stack trace none

Description Andreas Steinwachs CLA 2010-08-23 08:24:56 EDT
Build Identifier: Eclipse Persistence Services - 2.2.0.v20100818-r8059 (on GlassFish 3.0.1)

It seems to be impossible to catch any database exceptions, such as the EntityExistsException or other subclasses of PersistenceException.

Example: When inserting two different entities with equal values for a field annotated as unique, mySQL (I'm working with version 5.5.5-m3, just to be complete) throws a MySQLIntegrityConstraintViolationException. The latter is caught in DatabaseAccessor.executeDirectNoSelect (from line 793):

} catch (SQLException exception) {
    if (!getPlatform().shouldIgnoreException(exception)) {
        DatabaseException commException =
            processExceptionForCommError(session, exception, call);
        if (commException != null) throw commException;
        throw DatabaseException.sqlException(exception, this, session, false);
    }
} 

The DatabaseException is propagated further up the stack, but it never reaches my own application code, so that the app will never get notified of the problem and just fails.

In my example (test case attached), I am simply creating two instances of "SimpleEntity" with the same non-primary-key values and persist them. Now there's actually two issues with this:

First, as a side note, even though "mustBeUnique" is annotated with @Column(unique = true), EclipseLink seems to ignore this. In my opinion, some kind of "NonUniqueFieldException" should be thrown.

Secondly, and this is the main issue, the mySQL exception is caught as explained above but not propagated to the executed line of code of my application. Am I totally missing something here? Unless I have just missed something, this is a major problem, as I cannot catch database related exceptions anywhere. I'm setting the severity to normal anyway.

Reproducible: Always

Steps to Reproduce:
Please refer to the example stacktrace and test case attached to this bug.
Comment 1 Andreas Steinwachs CLA 2010-08-23 08:27:36 EDT
Created attachment 177207 [details]
The test case
Comment 2 Andreas Steinwachs CLA 2010-08-23 08:28:57 EDT
Created attachment 177208 [details]
The stack trace
Comment 3 Andreas Steinwachs CLA 2010-08-24 11:10:11 EDT
Forget the paragraph with the UniqueConstraint/unique annotation attribute. I'm never using schema generation and simply had forgotten that this is only valid for schema generation, of course. I'm still wondering why the exceptions don't get propagated to my application code when accessing the EntityManager.

Thanks in advance.
Comment 4 Andreas Steinwachs CLA 2010-08-26 09:17:38 EDT
Well... if I change SimpleDAO.
Comment 5 Andreas Steinwachs CLA 2010-08-26 09:21:13 EDT
Well... if I change the content of SimpleDAO.persist(...) to:

(...)
    try {
        em.persist(entity);
        em.flush();
    } 

I can catch the exceptions. It seems to be the intended behavior with JTA and not a bug of JPA. Is that correct?
Comment 6 Tom Ware CLA 2010-08-27 10:38:10 EDT
Closing.

You are correct.  In JPA database interaction for writes is only guaranteed to happen at flush or transaction commit.  In most cases, that will be when you see database exceptions.  The exception to that rule is that depending on your flush mode, certain read queries will also potentially trigger database interaction - i.e. they will trigger a flush even though one has not been explicitly called.
Comment 7 Eclipse Webmaster CLA 2022-06-09 10:30:38 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink