I have an interceptor defined which wraps my EJB method invocations in a try block with the intention of catching the database exceptions and passing more meaningful exceptions back to the EJB method caller.
I can clearly see from the stack trace that org.postgresql.util.PSQLException is being thrown at some point during the execution, so I designated it as an application exception in ejb-jar.xml.
What I expect to happen is that I can catch PSQLException directly after my call to proceed() in my interceptor, but what is actually happening is that PSQLException is buried two exceptions deep in the exception that I do catch there. I am getting:
javax.persistence.PersistenceException
containing a: oracle.toplink.essentials.exceptions.DatabaseException
containing a: org.postgresql.util.PSQLException
The following is my ejb-jar.xml and interceptor source:
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar
xmlns="
http://java.sun.com/xml/ns/javaee"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
version="3.0">
<interceptors>
<interceptor>
<interceptor-class>custaddr.exceptions.SQLExceptionHandler</interceptor-class>
</interceptor>
</interceptors>
<assembly-descriptor>
<interceptor-binding>
<ejb-name>*</ejb-name>
<interceptor-class>custaddr.exceptions.SQLExceptionHandler</interceptor-class>
</interceptor-binding>
<application-exception>
<exception-class>org.postgresql.util.PSQLException</exception-class>
<rollback>true</rollback>
</application-exception>
</assembly-descriptor>
</ejb-jar>
public class SQLExceptionHandler {
@AroundInvoke
public Object handleException( InvocationContext ctx ) throws Exception {
int errorCode;
LogUtil.log( "SQLException: Begin", Level.INFO, null );
try {
return ctx.proceed();
}
/* ** THIS IS NOT WORKING BECAUSE PSQLException IS BURIED **
catch ( PSQLException s ) {
// Get the error code then throw some specific exception based on that.
throw new CustomSpecificException();
}
*/
catch( Exception s ) {
LogUtil.log( "SQLException: Class: " + s.getClass(), Level.INFO, null );
LogUtil.log( "SQLException: Message: " + s.getLocalizedMessage(), Level.INFO, null );
LogUtil.log( "SQLException: String: " + s.toString(), Level.INFO, null );
/* PSQLException shows up when this statement prints */
LogUtil.log( "SQLException: Cause Cause Class: " + s.getCause().getCause().getClass(), Level.INFO, null );
throw s;
}
}
}
Am I completely misunderstanding how application exceptions are supposed to work, or I am just doing something wrong?
This is running on Glassfish 2 with Toplink connecting to a PostgreSQL database.
Any help is greatly appreciated!
Thanks,
Kurz
[Message sent by forum member 'kurzweil4' (kurzweil4)]
http://forums.java.net/jive/thread.jspa?messageID=320287