unwrapping InvocationTargetException in PGXAConnection.ConnectionHandler
unwrapping InvocationTargetException in PGXAConnection.ConnectionHandler
От:
Florent Guillaume <fg@nuxeo.com>
Дата:
Hi,
Using a Connection from an XAConnection, we've come across some stack
traces looking like:
Caused by: java.lang.reflect.UndeclaredThrowableException
at $Proxy26.prepareStatement(Unknown Source)
at com.example.MyClass.myMethod(MyClass.java:123)
...
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor47.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.postgresql.xa.PGXAConnection$ConnectionHandler.invoke(PGXAConnection.java:146)
... 156 more
Caused by: org.postgresql.util.PSQLException: This connection has been closed.
at org.postgresql.jdbc2.AbstractJdbc2Connection.checkClosed(AbstractJdbc2Connection.java:714)
at org.postgresql.jdbc3.AbstractJdbc3Connection.prepareStatement(AbstractJdbc3Connection.java:274)
at org.postgresql.jdbc2.AbstractJdbc2Connection.prepareStatement(AbstractJdbc2Connection.java:198)
at sun.reflect.GeneratedMethodAccessor47.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.postgresql.ds.jdbc23.AbstractJdbc23PooledConnection$ConnectionHandler.invoke(AbstractJdbc23PooledConnection.java:347)
at $Proxy26.prepareStatement(Unknown Source)
... 160 more
The code in myMethod looks like:
Connection connection = xaconnection.getConnection();
PreparedStatement ps = null;
try {
ps = connection.prepareStatement(sql);
... // use ps
} catch (SQLException e) {
... // some cleanup
throw new MyException("Could not select: " + sql, e);
} finally {
... // close ps
}
So the issue is that although we try to catch SQLException, what's
actually thrown is an UndeclaredThrowableException wrapping a
InvocationTargetException wrapping the underlying PSQLException.
I believe that to fix this, in PGXAConnection.ConnectionHandler the code doing:
return method.invoke(con, args);
should actually be:
try {
return method.invoke(con, args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
What do you think?
Regards,
Florent
--
Florent Guillaume, Director of R&D, Nuxeo
Open Source, Java EE based, Enterprise Content Management (ECM)
http://www.nuxeo.com http://www.nuxeo.org +33 1 40 33 79 87
Re: unwrapping InvocationTargetException in PGXAConnection.ConnectionHandler
От:
Kris Jurka <books@ejurka.com>
Дата:
On Fri, 11 Mar 2011, Florent Guillaume wrote: > Using a Connection from an XAConnection, we've come across some stack > traces looking like: > > Caused by: java.lang.reflect.UndeclaredThrowableException > > So the issue is that although we try to catch SQLException, what's > actually thrown is an UndeclaredThrowableException wrapping a > InvocationTargetException wrapping the underlying PSQLException. > Yes, your analysis is correct. This was previously fixed in CVS, but hasn't made it to a released version yet. Kris Jurka