unwrapping InvocationTargetException in PGXAConnection.ConnectionHandler - Mailing list pgsql-jdbc

From Florent Guillaume
Subject unwrapping InvocationTargetException in PGXAConnection.ConnectionHandler
Date
Msg-id AANLkTiky+UU1AzCo4UOQznDB8b8EbbNyB_M7YPHuw8B3@mail.gmail.com
Whole thread Raw
Responses Re: unwrapping InvocationTargetException in PGXAConnection.ConnectionHandler
List 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

pgsql-jdbc by date:

Previous
From: Dave Cramer
Date:
Subject: Re: PreparedStatement batch statement impossible
Next
From: Sean Bowman
Date:
Subject: Problem with executeBatch and "A result was returned when none was expected"