users@glassfish.java.net

How does one retrieve the underlying reason for a PersistenceException

From: <glassfish_at_javadesktop.org>
Date: Mon, 02 Apr 2007 17:10:35 PDT

We are using container managed persistance using EJB 3.0. The underlying database has a unique constraint. When we create an entity that violates the constraint, a PersistanceException is thrown. What we cannot seem to find out is what is the underlying database exception. Note that we are not violating the uniqueness of the entity ID, but rather an addtional unique constrant.

What I'd like to be able to do is to get the underlying sqlState so that I can look at that code and determine the real cause for the exception.

Currently we are catching the exception after the transaction is rolled back as an EJBTransactionRollbackException. One strange this is that this exception has a "getCausedByException" that you would think would have a nested exception but this is always null. We currently call "getCause" which returns a TransactionRolledbackLocalException. On this instance we need to call "getCausedByException" that returns PersistenceException. On that instance, we can call "getCause" but that returns a Toplink DatabaseException that we don't have in our build environment. From there, a "getCause" will give a SQLException, but that is "org.apache.derby.client.am.SqlException" and not a standard SqlException class.

So how does one get the underlying database exception in a standard portable way?
[Message sent by forum member 'bbergquist' (bbergquist)]

http://forums.java.net/jive/thread.jspa?messageID=210966