Uploaded image for project: 'Agroal'
  1. Agroal
  2. AG-196

[spring] AgroalDataSource and DataSourceUnwrapper not working

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major Major
    • 1.17, 2.1
    • None
    • spring
    • None
    • False
    • None
    • False
    • If Release Note Needed, Set a Value
    • Set a Value

      I have a io.agroal.springframework.boot.AgroalDataSource and I wrap it into another DataSource like DelegatingDataSource, for example. After that I need to unwrap it to retrieve the AgroalDataSource. For this purpose I tried DataSourceUnwrapper doing the following:

       

      DelegatingDataSource delegatingDataSource = ... //wrap AgraolDataSource
      ...
      AgroalDataSource agroalDataSource = DataSourceUnwrapper.unwrap(delegatingDataSource , io.agroal.api.AgroalDataSource.class);
      

      But I get null.

      This is due to implementation of methods isWrapperFor and unwrap in class io.agroal.springframework.boot.AgroalDataSource are delegating into the underlying datasource.

      I have a workaround, extending io.agroal.springframework.boot.AgroalDataSource, and implemeting these methods:

      public class MyAgroalDataSource extends io.agroal.springframework.boot.AgroalDataSource {
      
        @Override
        @SuppressWarnings("unchecked")
        public <T> T unwrap(Class<T> iface) throws SQLException {
          if (iface.isInstance(this)) {
            return (T) this;
          }
          return super.unwrap(iface);
        }
      
        @Override
        public boolean isWrapperFor(Class<?> iface) throws SQLException {
          return iface.isInstance(this) || super.isWrapperFor(iface);
        }
      
      }
      

      But It will be nice if Agroal take into account this use case and apply this changes in its source code.
       

              lbarreiro-1 Luis Barreiro
              oscarburgos-martin Óscar Burgos (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: