Community
Participate
Working Groups
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.
Created attachment 177207 [details] The test case
Created attachment 177208 [details] The stack trace
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.
Well... if I change SimpleDAO.
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?
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.
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink