unwrapping InvocationTargetException in PGXAConnection.ConnectionHandler

From: Florent Guillaume <fg(at)nuxeo(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: unwrapping InvocationTargetException in PGXAConnection.ConnectionHandler
Date: 2011-03-11 17:21:12
Message-ID: AANLkTiky+UU1AzCo4UOQznDB8b8EbbNyB_M7YPHuw8B3@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

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

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Sean Bowman 2011-03-16 09:14:35 Problem with executeBatch and "A result was returned when none was expected"
Previous Message Dave Cramer 2011-03-09 11:11:37 Re: PreparedStatement batch statement impossible