Bug 59569

Summary: isWrapperFor/unwrap implementations incorrect
Product: Tomcat Modules Reporter: Max Bolingbroke <batterseapower>
Component: jdbc-poolAssignee: Tomcat Developers Mailing List <dev>
Status: NEW ---    
Severity: normal    
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: All   

Description Max Bolingbroke 2016-05-18 20:37:10 UTC
In org.apache.tomcat.jdbc.pool.DataSourceProxy, isWrapperFor/unwrap always return false/null respectively. This is a spec violation, not least becaues unwrap should throw instead of returning null.

A similar issue affects org.apache.tomcat.jdbc.pool.ProxyConnection.

Correct implementations of these functions should look something like this (for DataSourceProxy):

   @SuppressWarnings("unused") // Has to match signature in DataSource
   public boolean isWrapperFor(Class<?> iface) throws SQLException {
       return poolProperties.getDataSource() instanceof DataSource && ((DataSource)poolProperties.getDataSource()).isWrapperFor(iface);
   }


   @SuppressWarnings("unused") // Has to match signature in DataSource
   public <T> T unwrap(Class<T> iface) throws SQLException {
       if (poolProperties.getDataSource() instanceof DataSource) {
           return ((DataSource)poolProperties.getDataSource()).unwrap(iface);
       } else {
           throw new SQLException("Not a wrapper for " + iface);
       }
   }

And for ProxyConnection:

   public boolean isWrapperFor(Class<?> iface) throws SQLException {
       if (iface == XAConnection.class && connection.getXAConnection()!=null) {
           return true;
       } else {
           return connection.getConnection().isWrapperFor(iface);
       }
   }


   public Object unwrap(Class<?> iface) throws SQLException {
       if (iface == PooledConnection.class) {
           return connection;
       }else if (iface == XAConnection.class) {
           return connection.getXAConnection();
       } else {
           return connection.getConnection().unwrap(iface);
       }
   }