iamazy / elasticsearch-sql

parse sql into elasticsearch dsl with antlr4
https://iamazy.github.io/elasticsearch-sql/
MIT License
340 stars 89 forks source link

去除私有构造函数 #68

Closed jackllvv closed 2 years ago

jackllvv commented 2 years ago

使用datasource连接时,会通过driverClass.newInstance()来初始化,如果构造方法私有会导致报错。

iamazy commented 2 years ago

可以给下你的jdbc使用的代码以及错误吗,具体的jdbc使用方式可以见 jdbc test

jackllvv commented 2 years ago

我想和mybatis进行整合,需要使用datasource,常见的数据源例如hikari,druid都会调用driver的构造方法。 贴上一段kikari的代码:


      Class<?> driverClass = null;
      ClassLoader threadContextClassLoader = Thread.currentThread().getContextClassLoader();
      try {
         if (threadContextClassLoader != null) {
            try {
               driverClass = threadContextClassLoader.loadClass(driverClassName);
               LOGGER.debug("Driver class {} found in Thread context class loader {}", driverClassName, threadContextClassLoader);
            }
            catch (ClassNotFoundException e) {
               LOGGER.debug("Driver class {} not found in Thread context class loader {}, trying classloader {}",
                            driverClassName, threadContextClassLoader, this.getClass().getClassLoader());
            }
         }

         if (driverClass == null) {
            driverClass = this.getClass().getClassLoader().loadClass(driverClassName);
            LOGGER.debug("Driver class {} found in the HikariConfig class classloader {}", driverClassName, this.getClass().getClassLoader());
         }
      } catch (ClassNotFoundException e) {
         LOGGER.error("Failed to load driver class {} from HikariConfig class classloader {}", driverClassName, this.getClass().getClassLoader());
      }

      if (driverClass == null) {
         throw new RuntimeException("Failed to load driver class " + driverClassName + " in either of HikariConfig class loader or Thread context classloader");
      }

      try {
         driverClass.newInstance();
         this.driverClassName = driverClassName;
      }
      catch (Exception e) {
         throw new RuntimeException("Failed to instantiate class " + driverClassName, e);
      }